Intent Messaging and resolution in Android
Intents can be divided into two groups:
- Explicit intents designate the target component by its name (the component name field, mentioned earlier, has a value set). Since component names would generally not be known to developers of other applications, explicit intents are typically used for application-internal messages â€” such as an activity starting a subordinate service or launching a sister activity.
Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications.
Android delivers an explicit intent to an instance of the designated target class. Nothing in the Intent object other than the component name matters for determining which component should get the intent.
A different strategy is needed for implicit intents. In the absence of a designated target, the Android system must find the best component (or components) to handle the intent â€” a single activity or service to perform the requested action or the set of broadcast receivers to respond to the broadcast announcement. It does so by comparing the contents of the Intent object to intent filters, structures associated with components that can potentially receive intents. Filters advertise the capabilities of a component and delimit the intents it can handle. They open the component to the possibility of receiving implicit intents of the advertised type. If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.
Only three aspects of an Intent object are consulted when the object is tested against an intent filter:
data (both URI and data type)
The extras and flags play no part in resolving which component receives an intent.
To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive. It, in effect, filters in intents of a desired type, while filtering out unwanted intents â€” but only unwanted implicit intents (those that don't name a target class). An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.
A component has separate filters for each job it can do, each face it can present to the user. For example, the NoteEditor activity of the sample Note Pad application has two filters â€” one for starting up with a specific note that the user can view or edit, and another for starting with a new, blank note that the user can fill in and save. (All of Note Pad's filters are described in the Note Pad Example section, later.)
Filters and security
An intent filter cannot be relied on for security. While it opens a component to receiving only certain kinds of implicit intents, it does nothing to prevent explicit intents from targeting the component. Even though a filter restricts the intents a component will be asked to handle to certain actions and data sources, someone could always put together an explicit intent with a different action and data source, and name the component as the target.
An intent filter is an instance of the
IntentFilterclass. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as
elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling
Context.registerReceiver(); they are directly created as IntentFilter objects.)
A filter has fields that parallel the action, data, and category fields of an Intent object. An implicit intent is tested against the filter in all three areas. To be delivered to the component that owns the filter, it must pass all three tests. If it fails even one of them, the Android system won't deliver it to the component â€” at least not on the basis of that filter. However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another.
Each of the three tests is described in detail below:
- Action test
element in the manifest file lists actions as
subelements. For example:
. . . > android:name="com.example.project.SHOW_CURRENT" /> android:name="com.example.project.SHOW_RECENT" /> android:name="com.example.project.SHOW_PENDING" /> . . .
As the example shows, while an Intent object names just a single action, a filter may list more than one. The list cannot be empty; a filter must contain at least one
element, or it will block all intents.
To pass this test, the action specified in the Intent object must match one of the actions listed in the filter. If the object or the filter does not specify an action, the results are as follows:
- If the filter fails to list any actions, there is nothing for an intent to match, so all intents fail the test. No intents can get through the filter.
On the other hand, an Intent object that doesn't specify an action automatically passes the test â€” as long as the filter contains at least one action.
- Category test
element also lists categories as subelements. For example:
. . . > android:name="android.intent.category.DEFAULT" /> android:name="android.intent.category.BROWSABLE" /> . . .
Note that the constants described earlier for actions and categories are not used in the manifest file. The full string values are used instead. For instance, the "
android.intent.category.BROWSABLE" string in the example above corresponds to the
CATEGORY_BROWSABLEconstant mentioned earlier in this document. Similarly, the string "
android.intent.action.EDIT" corresponds to the
For an intent to pass the category test, every category in the Intent object must match a category in the filter. The filter can list additional categories, but it cannot omit any that are in the intent.
In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter. That's mostly true. However, with one exception, Android treats all implicit intents passed to
startActivity()as if they contained at least one category: "
CATEGORY_DEFAULTconstant). Therefore, activities that are willing to receive implicit intents must include "
android.intent.category.DEFAULT" in their intent filters. (Filters with "
android.intent.action.MAIN" and "
android.intent.category.LAUNCHER" settings are the exception. They mark activities that begin new tasks and that are represented on the launcher screen. They can include "
android.intent.category.DEFAULT" in the list of categories, but don't need to.) See Using intent matching, later, for more on these filters.)
- Data test
Like the action and categories, the data specification for an intent filter is contained in a subelement. And, as in those cases, the subelement can appear multiple times, or not at all. For example:
. . . > android:mimeType="video/mpeg" android:scheme="https" . . . /> android:mimeType="audio/mpeg" android:scheme="https" . . . /> . . .
For example, in the following URI,
the scheme is "
content", the host is "
com.example.project", the port is "
200", and the path is "
folder/subfolder/etc". The host and port together constitute the URI authority; if a host is not specified, the port is ignored.
Each of these attributes is optional, but they are not independent of each other: For an authority to be meaningful, a scheme must also be specified. For a path to be meaningful, both a scheme and an authority must be specified.
When the URI in an Intent object is compared to a URI specification in a filter, it's compared only to the parts of the URI actually mentioned in the filter. For example, if a filter specifies only a scheme, all URIs with that scheme match the filter. If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority match, regardless of their paths. If a filter specifies a scheme, an authority, and a path, only URIs with the same scheme, authority, and path match. However, a path specification in the filter can contain wildcards to require only a partial match of the path.
typeattribute of a
element specifies the MIME type of the data. It's more common in filters than a URI. Both the Intent object and the filter can use a "*" wildcard for the subtype field â€” for example, "
text/*" or "
audio/*" â€” indicating any subtype matches.
The data test compares both the URI and the data type in the Intent object to a URI and data type specified in the filter. The rules are as follows:
- An Intent object that contains neither a URI nor a data type passes the test only if the filter likewise does not specify any URIs or data types.
An Intent object that contains a URI but no data type (and a type cannot be inferred from the URI) passes the test only if its URI matches a URI in the filter and the filter likewise does not specify a type. This will be the case only for URIs like
tel:that do not refer to actual data.
An Intent object that contains a data type but not a URI passes the test only if the filter lists the same data type and similarly does not specify a URI.
An Intent object that contains both a URI and a data type (or a data type can be inferred from the URI) passes the data type part of the test only if its type matches a type listed in the filter. It passes the URI part of the test either if its URI matches a URI in the filter or if it has a
file:URI and the filter does not specify a URI. In other words, a component is presumed to support
file:data if its filter lists only a data type.
If an intent can pass through the filters of more than one activity or service, the user may be asked which component to activate. An exception is raised if no target can be found.
The last rule shown above for the data test, rule (d), reflects the expectation that components are able to get local data from a file or content provider. Therefore, their filters can list just a data type and do not need to explicitly name the
file:schemes. This is a typical case. A
element like the following, for example, tells Android that the component can get image data from a content provider and display it:
Since most available data is dispensed by content providers, filters that specify a data type but not a URI are perhaps the most common.
Another common configuration is filters with a scheme and a data type. For example, a
element like the following tells Android that the component can get video data from the network and display it:
android:scheme="https" android:type="video/*" />
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.
Most applications also have a way to start fresh, without a reference to any particular data. Activities that can initiate applications have filters with "
android.intent.action.MAIN" specified as the action. If they are to be represented in the application launcher, they also specify the "
. . . > android:name="code android.intent.action.MAIN" /> android:name="code android.intent.category.LAUNCHER" />
Using intent matching
Intents are matched against intent filters not only to discover a target component to activate, but also to discover something about the set of components on the device. For example, the Android system populates the application launcher, the top-level screen that shows the applications that are available for the user to launch, by finding all the activities with intent filters that specify the "
android.intent.action.MAIN" action and "
android.intent.category.LAUNCHER" category (as illustrated in the previous section). It then displays the icons and labels of those activities in the launcher. Similarly, it discovers the home screen by looking for the activity with "
android.intent.category.HOME" in its filter.
Your application can use intent matching is a similar way. The
PackageManagerhas a set of
query...()methods that return all components that can accept a particular intent, and a similar series of
resolve...()methods that determine the best component to respond to an intent. For example,
queryIntentActivities()returns a list of all activities that can perform the intent passed as an argument, and
queryIntentServices()returns a similar list of services. Neither method activates the components; they just list the ones that can respond. There's a similar method,
queryBroadcastReceivers(), for broadcast receivers.