Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Module 5: Introduction to Intent in Android
I. INTRODUCTION
Module 5 describes the usage of Intents to communicate between Android
components.
II. LEARNING OBJECTIVES
After the completion of this module, the students are expected to;
Define Intents
Identify types of Intents
Discover building an intent
Differentiate Implicit and Explicit Intents Through Examples
III. TOPICS AND KEY CONCEPTS
A. What is Intent in Android?
An 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.
Intents are used generally for starting a new activity using startActivity().
An Intent is a messaging object you can use to request an action from another
app component. Although intents facilitate communication between components
in several ways, there are three fundamental use cases:
Starting an activity
An Activity represents a single screen in an app. You can start a new
instance of an Activity by passing an Intent to startActivity(). The Intent
describes the activity to start and carries any necessary data.
If you want to receive a result from the activity when it finishes, call
startActivityForResult(). Your activity receives the result as a separate
Intent object in your activity's onActivityResult() callback. For more
information, see the Activities guide.
Starting a service
A Service is a component that performs operations in the background
without a user interface. With Android 5.0 (API level 21) and later, you
can start a service with JobScheduler. For more information about
JobScheduler, see its API-reference documentation.
For versions earlier than Android 5.0 (API level 21), you can start a
service by using methods of the Service class. You can start a service to
perform a one-time operation (such as downloading a file) by passing an
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 1 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Intent to startService(). The Intent describes the service to start and
carries any necessary data.
If the service is designed with a client-server interface, you can bind to
the service from another component by passing an Intent to
bindService(). For more information, see the Services guide.
Delivering a broadcast
A broadcast is a message that any app can receive. The system delivers
various broadcasts for system events, such as when the system boots up
or the device starts charging. You can deliver a broadcast to other apps
by passing an Intent to sendBroadcast() or sendOrderedBroadcast().
B. Intent Types
There are two types of intents:
Explicit intents specify which application will satisfy the intent, by
supplying either the target app's package name or a fully-qualified
component class name. You'll typically use an explicit intent to start a
component in your own app, because you know the class name of the
activity or service you want to start. For example, you might start a new
activity within your app in response to a user action, or start a service to
download a file in the background.
An explicit intent is an 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.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 2 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Implicit intents do not name a specific component, but instead declare
a general action to perform, which allows a component from another app
to handle it. For example, if you want to show the user a location on a
map, you can use an implicit intent to request that another capable app
show a specified location on a map.
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.
An Implicit intent specifies an action that can invoke any app on the
device to be able to perform an action. Using an Implicit Intent is useful
when your app cannot perform the action but other apps probably can
and you’d like the user to pick which app to use.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 3 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Figure 1 shows how an intent is used when starting an activity. When the Intent
object names a specific activity component explicitly, the system immediately
starts that component.
When you use an implicit intent, the Android system finds the appropriate
component to start by comparing the contents of the intent to the intent filters
declared in the manifest file of other apps on the device. If the intent matches an
intent filter, the system starts that component and delivers it the Intent object. If
multiple intent filters are compatible, the system displays a dialog so the user
can pick which app to use.
An intent filter is an expression in an app's manifest file that specifies the type
of intents that the component would like to receive. For instance, by declaring
an intent filter for an activity, you make it possible for other apps to directly
start your activity with a certain kind of intent. Likewise, if you do not declare
any intent filters for an activity, then it can be started only with an explicit
intent.
C. Building an Intent
An Intent object carries information that the Android system uses to determine
which component to start (such as the exact component name or component
category that should receive the intent), plus information that the recipient
component uses in order to properly perform the action (such as the action to
take and the data to act upon).
The primary information contained in an Intent is the following:
Component Name
Action
Data
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 4 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Category
Extras
Flags
Component Name
The name of the component to start.
This is optional, but it's the critical piece of information that makes an intent
explicit, meaning that the intent should be delivered only to the app component
defined by the component name. Without a component name, the intent is
implicit and the system decides which component should receive the intent
based on the other intent information (such as the action, data, and category—
described below). If you need to start a specific component in your app, you
should specify the component name.
This field of the Intent is a ComponentName object, which you can specify using
a fully qualified class name of the target component, including the package
name of the app, for example, com.example.ExampleActivity. You can set the
component name with setComponent(), setClass(), setClassName(), or with the
Intent constructor.
Action
A string that specifies the generic action to perform (such as view or pick).
In the case of a broadcast intent, this is the action that took place and is being
reported. The action largely determines how the rest of the intent is structured—
particularly the information that is contained in the data and extras.
You can specify your own actions for use by intents within your app (or for use
by other apps to invoke components in your app), but you usually specify action
constants defined by the Intent class or other framework classes. Here are some
common actions for starting an activity:
ACTION_VIEW - Use this action in an intent with startActivity() when
you have some information that an activity can show to the user, such as
a photo to view in a gallery app, or an address to view in a map app.
ACTION_SEND - Also known as the share intent, you should use this in
an intent with startActivity() when you have some data that the user can
share through another app, such as an email app or social sharing app.
Data
The URI (a Uri object) that references the data to be acted on and/or the MIME
type of that data. The type of data supplied is generally dictated by the intent's
action. For example, if the action is ACTION_EDIT, the data should contain the
URI of the document to edit.
When creating an intent, it's often important to specify the type of data (its
MIME type) in addition to its URI. For example, an activity that's able to display
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 5 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
images probably won't be able to play an audio file, even though the URI formats
could be similar. Specifying the MIME type of your data helps the Android
system find the best component to receive your intent. However, the MIME type
can sometimes be inferred from the URI—particularly when the data is a
content: URI. A content: URI indicates the data is located on the device and
controlled by a ContentProvider, which makes the data MIME type visible to the
system.
To set only the data URI, call setData(). To set only the MIME type, call setType().
If necessary, you can set both explicitly with setDataAndType().
Category
A string containing additional information about the kind of component that
should handle the intent. Any number of category descriptions can be placed in
an intent, but most intents do not require a category. Here are some common
categories:
CATEGORY_BROWSABLE = The target activity allows itself to be started
by a web browser to display data referenced by a link, such as an image
or an e-mail message.
CATEGORY_LAUNCHER - The activity is the initial activity of a task and
is listed in the system's application launcher.
You can specify a category with addCategory().
These properties listed above (component name, action, data, and category)
represent the defining characteristics of an intent. By reading these properties,
the Android system is able to resolve which app component it should start.
However, an intent can carry additional information that does not affect how it is
resolved to an app component. An intent can also supply the following
information:
Extras - Key-value pairs that carry additional information required to
accomplish the requested action. Just as some actions use particular
kinds of data URIs, some actions also use particular extras.
You can add extra data with various putExtra() methods, each accepting
two parameters: the key name and the value. You can also create a
Bundle object with all the extra data, then insert the Bundle in the Intent
with putExtras().
For example, when creating an intent to send an email with
ACTION_SEND, you can specify the to recipient with the EXTRA_EMAIL
key, and specify the subject with the EXTRA_SUBJECT key.
The Intent class specifies many EXTRA_* constants for standardized data
types. If you need to declare your own extra keys (for intents that your
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 6 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
app receives), be sure to include your app's package name as a prefix, as
shown in the following example:
Flags - Flags are defined in the Intent class that function as metadata for
the intent. The flags may instruct the Android system how to launch an
activity (for example, which task the activity should belong to) and how
to treat it after it's launched (for example, whether it belongs in the list of
recent activities).
D. Example of Explicit Intent
An explicit intent is one that you use to launch a specific app component, such
as a particular activity or service in your app. To create an explicit intent, define
the component name for the Intent object—all other intent properties are
optional.
For example, if you built a service in your app, named DownloadService,
designed to download a file from the web, you can start it with the following
code:
The Intent(Context, Class) constructor supplies the app Context and the
component a Class object. As such, this intent explicitly starts the
DownloadService class in the app.
E. Example of Implicit Intent
An implicit intent specifies an action that can invoke any app on the device able
to perform the action. Using an implicit intent is useful when your app cannot
perform the action, but other apps probably can and you'd like the user to pick
which app to use.
For example, if you have content that you want the user to share with other
people, create an intent with the ACTION_SEND action and add extras that
specify the content to share. When you call startActivity() with that intent, the
user can pick an app through which to share the content.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 7 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
When startActivity() is called, the system examines all of the installed apps to
determine which ones can handle this kind of intent (an intent with the
ACTION_SEND action and that carries "text/plain" data). If there's only one app
that can handle it, that app opens immediately and is given the intent. If
multiple activities accept the intent, the system displays a dialog such as the
one shown in Figure 2, so the user can pick which app to use.
F. Forcing an App Chooser
When there is more than one app that responds to your implicit intent, the user
can select which app to use and make that app the default choice for the action.
The ability to select a default is helpful when performing an action for which the
user probably wants to use the same app every time, such as when opening a
web page (users often prefer just one web browser).
However, if multiple apps can respond to the intent and the user might want to
use a different app each time, you should explicitly show a chooser dialog. The
chooser dialog asks the user to select which app to use for the action (the user
cannot select a default app for the action). For example, when your app performs
"share" with the ACTION_SEND action, users may want to share using a
different app depending on their current situation, so you should always use the
chooser dialog, as shown in Figure 2.
To show the chooser, create an Intent using createChooser() and pass it to
startActivity(), as shown in the following example. This example displays a dialog
with a list of apps that respond to the intent passed to the createChooser()
method and uses the supplied text as the dialog title.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 8 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Figure 2. A Chooser Dialog
G. Receiving an Implicit Intent
To advertise which implicit intents your app can receive, declare one or more
intent filters for each of your app components with an <intent-filter> element in
your manifest file. Each intent filter specifies the type of intents it accepts based
on the intent's action, data, and category. The system delivers an implicit intent
to your app component only if the intent can pass through one of your intent
filters.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 9 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
An app component should declare separate filters for each unique job it can do.
For example, one activity in an image gallery app may have two filters: one filter
to view an image, and another filter to edit an image. When the activity starts, it
inspects the Intent and decides how to behave based on the information in the
Intent (such as to show the editor controls or not).
Each intent filter is defined by an <intent-filter> element in the app's manifest
file, nested in the corresponding app component (such as an <activity> element).
Inside the <intent-filter>, you can specify the type of intents to accept using one
or more of these three elements:
<action>
Declares the intent action accepted, in the name attribute. The value
must be the literal string value of an action, not the class constant.
<data>
Declares the type of data accepted, using one or more attributes that
specify various aspects of the data URI (scheme, host, port, path) and
MIME type.
<category>
Declares the intent category accepted, in the name attribute. The value
must be the literal string value of an action, not the class constant.
For example, here's an activity declaration with an intent filter to receive an
ACTION_SEND intent when the data type is text:
You can create a filter that includes more than one instance of <action>, <data>,
or <category>. If you do, you need to be certain that the component can handle
any and all combinations of those filter elements.
When you want to handle multiple kinds of intents, but only in specific
combinations of action, data, and category type, then you need to create
multiple intent filters.
An implicit intent is tested against a filter by comparing the intent to each of the
three elements. To be delivered to the component, the intent must pass all three
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 10 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
tests. If it fails to match even one of them, the Android system won't deliver the
intent to the component. However, because a component may have multiple
intent filters, an intent that does not pass through one of a component's filters
might make it through on another filter. More information about how the system
resolves intents is provided in the section below about Intent Resolution.
IV. TEACHING AND LEARNING MATERIALS RESOURCE
Materials are being taken from the following;
Online Resources
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 11 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
V. LEARNING TASK
Instruction: Answer the following questions briefly, your
answer will be evaluated using the following rubric.
Holistic Rubric
Exceeding Meeting Approaching Below None
(10) (8) (6) (2) (0)
Substantial,
Content Clarity
specific, and/or
The presence of ideas Sufficient
illustrative
developed through developed Limited content Superficial
content No content
facts, examples, content with with inadequate and/or
demonstrating or answer
illustrations, details, adequate elaboration or minimal
strong provided.
opinions, statistics, elaboration or explanation content
development and
reasons, and/or explanation.
sophisticated
explanations.
ideas.
1. Identify at least two (2) issues about Intent in Android Application, describe each
concern, and discuss how to resolve these.
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
2. When do you think it is necessary to use Explicit and Implicit Intent in an
Android Application?
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
3. What types of data can we pass in Intent? How can we store and access it?
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Score Sheet
Question 1 Question 2 Question 3 Total Score
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 12 of 13
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
VI. REFERENCES
Online Resources
https://acadgild.com/blog/intent-in-android-introduction
https://developer.android.com/guide/components/intents-filters
https://www.vogella.com/tutorials/AndroidIntent/article.html
https://www.tutlane.com/tutorial/android/android-intents-implicit-explicit
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 13 of 13