The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
We have seen
Intent objects briefly, in our discussion of
having multiple activities in our application. However, we
really did not dive into too much of the details about those
objects, and they can be used in other ways besides starting up an activity.
In this chapter, we will examine
Intent and their filters.
When Sir Tim Berners-Lee cooked up the Hypertext Transfer Protocol — HTTP – he set up a system of verbs plus addresses in the form of URLs. The address indicated a resource, such as a Web page, graphic, or server-side program. The verb indicated what should be done: GET to retrieve it, POST to send form data to it for processing, etc.
Intent is similar, in that it represents an action plus context. There are
more actions and more components to the context with
Intent than there
are with HTTP verbs and resources, but the concept is still the same.
Just as a Web browser knows how to process a verb+URL pair, Android knows how
to find activities or other application logic that will handle a given
The two most important pieces of an
Intent are the action and what Android
refers to as the “data”. These are almost exactly analogous to HTTP verbs and
URLs — the action is the verb, and the “data” is a
Uri, such as
https://commonsware.com representing an HTTP URL to some balding guy’s
Actions are constants, such as
ACTION_VIEW (to bring up a viewer for the
ACTION_EDIT (to edit the resource).
If you were to create an
ACTION_VIEW with a content
https://commonsware.com, and pass that
Intent to Android via
Android would know to find and open an activity capable of viewing that resource.
There are other criteria you can place inside an
Intent, besides the
action and “data”
Uri, such as:
LAUNCHERcategory, indicating it should show up on the launcher menu. Other activities will probably be in the
DEFAULTcategory, though other categories exist and are used on occasion.
Bundleof other information you want to pass along to the receiver with the
Intent, that the recipient might want to take advantage of. What pieces of information a given recipient can use is up to the recipient and (hopefully) is well-documented.
You will find rosters of the standard actions, categories, and extras
in the Android SDK documentation for the
As noted above, if you specify the target component in your
Intent, Android has
no doubt where the
Intent is supposed to be routed to — it will launch the
named activity. This might be OK if the target recipient (e.g., the activity
to be started) is in your application.
It definitely is not recommended for invoking functionality in other applications.
Component names, by and large, are considered private to the application and
are subject to change. Actions,
Uri templates, and MIME types are the preferred
ways of identifying capabilities you wish third-party code to supply.
If you do not specify the target component, then Android has to figure out what
recipients are eligible to receive the
Intent. For example, Android
will take the
Intent you supply to
startActivity() and find the
activities that might support it. Note the
use of the plural “activities”, as a broadly-written intent might well resolve
to several activities. That is the… ummm… intent (pardon the pun), as you
will see later in this chapter.
This routing approach is referred to as implicit routing.
Basically, there are three rules, all of which must be true for a given
activity to be eligible for a given
The upshot is that you want to make your
Intent specific enough to find the
right recipient, and no more specific than that.
This will become clearer as we work through some examples throughout this chapter.
All Android components that wish to be started via an
Intent must declare
filters, so Android knows which intents should go to that component. A common
approach for this is to add one or more
<intent-filter> elements to your
AndroidManifest.xml file, inside the element for the component that should
respond to the
For example, all of the sample projects in this book have an
<activity> that looks like this:
<intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter>
Here, we declare that this activity:
LAUNCHERcategory, meaning it gets an icon in anything that thinks of itself as a “launcher”, such as the home screen
You are welcome to have more than one action or more than one category in your
Intent filters. That indicates that the associated component (e.g., activity)
handles multiple different sorts of
The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.
The preview of this section was whisked away by a shark-infested tornado.
The preview of this section was the victim of a MITM ('Martian in the middle') attack.
The preview of this section was lost in the sofa cushions.