Defining intents

Intents are made up of three primary pieces of information—action, categories, and data—and include an additional set of optional elements. An action is simply a String, as is a category, and data is defined in the form of a Uri object. A Uri is a generic URI (as defined by RFC 3986) which includes a scheme, an authority, and optionally a path (you will find out more about these parts in the next section). Table 4.1 lays out all of the components of an Intent object.

Table 4.1 Intent elements and description

Intent element



Extra data to pass to the Intent that is in the form of a Bundle


Specifies an explicit package and class to use for Intent, optional, normally inferred from action, type, and categories


Specifies an explicit MIME type (as opposed to being parsed from a URI)


Additional metadata about Intent (for example, android.intent.category.LAUNCHER)


Data to work with expressed as a URI (for example, content://contacts/1)


Fully qualified String indicating action (for example, android.intent.action.MAIN)

Intent definitions typically express a combination of action, data, and attributes such as category. This designation is used by the system as a sort of language to resolve exactly which class should be used to fill the request.

When a component such as an Activity wants to call upon an Intent, it can do so in one of two ways:

■ Implicit Intent invocation

■ Explicit Intent invocation

An implicit Intent invocation is one in which the platform determines which component is the best to run the Intent. This happens through a process of Intent resolution using the action, data, and categories. We will explore this resolution process in detail in the next section. An explicit Intent invocation is one in which the code directly specifies which component should handle the Intent. Explicit invocation is done by specifying either the Class or ComponentName of the receiver (where ComponentName is a String for the package and a String for the class).

To explicitly invoke an Intent, you can use the following form: Intent(Context ctx, Class cls). With this approach you can short-circuit all the Android Intent-resolution wiring and directly pass in an Activity or Service class reference to handle the Intent. While this approach is convenient and fast, and therefore sometimes arguably appropriate, it also introduces tight coupling that may be a disadvantage later.

In listing 4.1 we show the final portion of the ReviewDetail Activity from the RestaurantFinder sample application. This listing shows several implicit Intent invocations. (We began this application in chapter 3; the first half of this class is shown in listing 3.6.)

0 0

Post a comment