100% found this document useful (1 vote)
123 views

Ch3 Intent and Service

This chapter discusses intents, services, and event handling in Android. It covers: 1. The three main ways of handling events: creating anonymous classes, implementing interfaces, or declaring handlers in XML. 2. Intents allow communication between Android components like activities and services. There are explicit intents for specific components and implicit intents for general actions. 3. Services are used for long-running background tasks without direct user interaction. Started services run indefinitely, while bound services allow interaction across processes.

Uploaded by

Abni boo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
123 views

Ch3 Intent and Service

This chapter discusses intents, services, and event handling in Android. It covers: 1. The three main ways of handling events: creating anonymous classes, implementing interfaces, or declaring handlers in XML. 2. Intents allow communication between Android components like activities and services. There are explicit intents for specific components and implicit intents for general actions. 3. Services are used for long-running background tasks without direct user interaction. Started services run indefinitely, while bound services allow interaction across processes.

Uploaded by

Abni boo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter Three

Intents and Service


EVENT HANDLING
• The action of clicking a Button, pressing the Enter key,
or performing any action on any control is considered
an event.
• The reaction to the event, that is, the action to be
taken when the event occurs, is called event handling.
• To handle an event, you use the listeners that wait for
an event occurrence.
• When an event occurs, the listeners detect it and
direct the program to the appropriate routine
Con’t
• An event listener is an interface in the View class that
contains a single callback method, called an event
occurrence.
• For example the callback method onClick() is called when
the user clicks on a button.
• For event handling, the event listener is either
implemented in the Activity class or is defined as an
anonymous class.
• There after, an instance of the implementation is passed to
the respective control through the setOnClickListener()
method.
Note: Click is just one type of an event.

There are three ways of event handling:

• Creating an anonymous inner class


• Implementing the OnClickListener interface
• Declaring the event handler in the XML
definition of the control
Toast
• Toast & Custom Toast With Example In Android Studio:
• In Android, Toast is used to display information for a period of time.
• It contains a message to be displayed quickly and disappears after
specified period of time.
• It does not block the user interaction. Toast is a subclass of Object
class.
• In this we use two constants for setting the duration for the Toast.
• Toast notification in android always appears near the bottom of the
screen.
• We can also create our custom toast by using custom layout(xml
file).
Important Methods Of Toast:
1. makeText(Context context, CharSequence text, int
duration):
• This method is used to initiate the Toast. This
method take three parameters
• (A) First is for the application Context,(b) Second is
text message and (c)last one is duration for the
Toast.
• Constants of Toast: Below is the constants of Toast
that are used for setting the duration for the Toast.
Con’t
1.LENGTH_LONG: It is used to display the Toast
for a long period of time. When we set this
duration the Toast will be displayed for a long
duration.
2. LENGTH_SHORT: It is used to display the Toast
for short period of time. When we set this
duration the Toast will be displayed for short
duration.
Con’t
• Below we show the use of makeText() method
of Toast in which we set application context, a
text message and duration for the Toast.
• Toast toast =
Toast.makeText(getApplicationContext(),
"Simple Toast", Toast.LENGTH_LONG); //
initiate the Toast with context, message and
duration for the Toast
Con’t
• In Android, Toast is used when we required to
notify user about an operation without
expecting any user input.
• It displays a small popup for message and
automatically fades out after timeout.
2. show()
• This method is used to display the Toast on the screen.
This method is display the text which we create using
makeText() method of Toast.
• Below we Firstly initiate the Toast and then display it
using show() method.
• Toast toast = Toast.makeText(getApplicationContext(),
"Simple Toast In Android", Toast.LENGTH_LONG); //
initiate the Toast with context, message and duration
for the Toast
• toast.show(); // display the Toast
3. setGravity(int,int,int)
• This method is used to set the gravity for the Toast. This
method accepts three parameters: a Gravity constant, an x-
position offset, and a y-position offset.
• Below we Firstly initiate the Toast, set top and left gravity and
then display it using show() method.
• Toast toast = Toast.makeText(getApplicationContext(), "Simple
Toast In Android", Toast.LENGTH_LONG); // initiate the Toast
with context, message and duration for the Toast
• toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); // set
gravity for the Toast.
• toast.show(); // display the Toast
4. setText(CharSequence s)
• This method is used to set the text for the Toast. If we use
makeText() method and then we want to change the text value for
the Toast then we use this method.
• Below we firstly create a new Toast using makeText() method and
then set the text for the Toast.
• Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast
In Android", Toast.LENGTH_LONG); // initiate the Toast with context,
message and duration for the Toast
• toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); // set gravity for
the Toast.
• toast.setText("Changed Toast Text"); // set the text for the Toast
• toast.show(); // display the Toast
5. setDuration(int duration):
• This method is used to set the duration for the Toast. If we use makeText()
method and then we want to change the duration for the Toast then we
use this method.
• Below we firstly create a new Toast using makeText() method and then set
the duration for the Toast.
• 16
• Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast In
Android", Toast.LENGTH_LONG); // initiate the Toast with context, message
and duration for the Toast
• toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); // set gravity for the
Toast.
• toast.setDuration(Toast.LENGTH_SHORT); // set the duration for the Toast.
• toast.show(); // display the Toast
6. inflate(int, ViewGroup):
• This method is used to inflate the layout from the xml. In
this method first parameter is the layout resource ID and
the second is the root View.
• Below we retrieve the Layout Inflater and then inflate the
layout from the xml file.
• // Retrieve the Layout Inflater and inflate the layout from
xml
• LayoutInflater inflater = getLayoutInflater();
• View layout = inflater.inflate(R.layout.custom_toast_layout,
• (ViewGroup) findViewById(R.id.toast_layout_root));
7. setView(View):
• This method is used to set the view for the Toast. In this method we pass the
inflated layout which we inflate using inflate() method.
• Below we firstly retrieve the layout inflater and then inflate the layout and
finally create a new Toast and pass the inflated layout in the setView() method.
• // Retrieve the Layout Inflater and inflate the layout from xml
• LayoutInflater inflater = getLayoutInflater();
• View layout = inflater.inflate(R.layout.custom_toast_layout,
• (ViewGroup) findViewById(R.id.toast_layout_root));
• // create a new Toast using context
• Toast toast = new Toast(getApplicationContext());
• toast.setDuration(Toast.LENGTH_LONG); // set the duration for the Toast
• toast.setView(layout); // set the inflated layout
• toast.show(); // display the custom Toast
Custom Toast in Android:

• In Android, Sometimes simple Toast may not


be satisfactory, and then we can go for
customizing a Toast.
• For creating a custom layout, define a View
layout, in XML and pass the root View object
to the setView(View) method.
Steps for Implementation of Custom Toast In Android:

• Step 1: Firstly Retrieve the Layout Inflater with getLayoutInflater()


(or getSystemService()) and then inflate the layout from XML
using inflate(int, ViewGroup). In inflate method first parameter is
the layout resource ID and the second is the root View.
• Step 2: Create a new Toast with Toast(Context) and set some
properties of the Toast, such as the duration and gravity.
• Step 3: Call setView(View) and pass the inflated layout in this
method.
• Step 4: Display the Toast on the screen using show() method of
Toast.
• In the below example we have shown the functioning of Toast and
custom Toast both.
INTENTS
• Intent is a simple message object that is used
to communicate between android
components such as activities, content
providers, broadcast receivers and services.
• Intents are also used to transfer data between
activities.
Use of Intents:

• Starting an activity
• Starting a service
• Delivering a broadcast
Types of Intent

1 Explicit Intent
2 Implicit Intent
An explicit intent
is Intent where you explicitly define the component
that needs to be called by the Android System.
An explicit intent is one that you can use to launch a
specific app component, such as a particular
activity or service in your app
Intent i= new Intent (this, MainActivity.class);
startActivity(i);
Thus specifying that we want to navigate to
MainActivity.class.
The implicit intent
• The implicit intent is the intent where instead
of defining the exact components, you define
the action that you want to perform for
different activities.
Intent i= new Intent(Intent.ACTION_VIEW);
i=setData(Uri.parse(“http://www.uu.edu.et.com));
startActivity(i);
Thus asking the system to find a suitable componet to
run this URI.
Android Service
• Service is s long running task or process
without any user interaction.
• The android.app.Service is sub class of Context
Wrapper class.
• A Service can essentially take two forms
Started Service
Bound Service
Started Service
• A service is “started” when an application
component starts it by calling startService().
• Once started ,a service can run in the
background indefinitely, even if the
component that stared it is destroyed.
• Started service perform a single operation
and does not return a result to the caller.
Bound Service
• A service is bound when an application
component binds to it by calling bindService().
• A bound service allows components to
interact with the service, send request, get
result and event do so across processes with
interprocess communication(IPC).
• Abound Service runs only as long as another
application component is bound to it.
Con’t
• Multiple component can bind to the service
at once, but when all of them unbind the
service is destroyed.
Thank you

You might also like