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.
The preview of this section is in the process of being translated from its native Klingon.
The preview of this section will not appear here for a while, due to a time machine mishap.
The preview of this section took that left turn at Albuquerque.
The preview of this section was traded for a bag of magic beans.
The preview of this section left for Hollywood to appear in a reality TV show.