Class Overview
An intent is an abstract description of an operation to be performed. It can be used with startActivity
to launch anActivity
, broadcastIntent
to send it to any interested BroadcastReceiver
components, and startService(Intent)
orbindService(Intent, ServiceConnection, int)
to communicate with a background Service
.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:
-
action -- The general action to be performed, such as
ACTION_VIEW
,ACTION_EDIT
,ACTION_MAIN
, etc. -
data -- The data to operate on, such as a person record in the contacts database, expressed as a
Uri
.
Some examples of action/data pairs are:
-
ACTION_VIEW
content://contacts/people/1 -- Display information about the person whose identifier is "1". -
ACTION_DIAL
content://contacts/people/1 -- Display the phone dialer with the person filled in. -
ACTION_VIEW
tel:123 -- Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI. -
ACTION_DIAL
tel:123 -- Display the phone dialer with the given number filled in. -
ACTION_EDIT
content://contacts/people/1 -- Edit information about the person whose identifier is "1". -
ACTION_VIEW
content://contacts/people/ -- Display a list of people, which the user can browse through. This example is a typical top-level entry into the Contacts application, showing you the list of people. Selecting a particular person to view would result in a new intent {ACTION_VIEW
content://contacts/N } being used to start an activity to display that person.
In addition to these primary attributes, there are a number of secondary attributes that you can also include with an intent:
-
category -- Gives additional information about the action to execute. For example,
CATEGORY_LAUNCHER
means it should appear in the Launcher as a top-level application, whileCATEGORY_ALTERNATIVE
means it should be included in a list of alternative actions the user can perform on a piece of data. -
type -- Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.
-
component -- Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
-
extras -- This is a
Bundle
of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.
Here are some examples of other operations you can specify as intents using these additional parameters:
-
ACTION_MAIN
with categoryCATEGORY_HOME
-- Launch the home screen. -
ACTION_GET_CONTENT
with MIME typevnd.android.cursor.item/phone
-- Display the list of people's phone numbers, allowing the user to browse through them and pick one and return it to the parent activity. -
ACTION_GET_CONTENT
with MIME type */* and categoryCATEGORY_OPENABLE
-- Display all pickers for data that can be opened withContentResolver.openInputStream()
, allowing the user to pick one of them and then some data inside of it and returning the resulting URI to the caller. This can be used, for example, in an e-mail application to allow the user to pick some data to include as an attachment.
There are a variety of standard Intent action and category constants defined in the Intent class, but applications can also define their own. These strings use java style scoping, to ensure they are unique -- for example, the standard ACTION_VIEW
is called "android.intent.action.VIEW".
Put together, the set of actions, data types, categories, and extra data defines a language for the system allowing for the expression of phrases such as "call john smith's cell". As applications are added to the system, they can extend this language by adding new actions, types, and categories, or they can modify the behavior of existing phrases by supplying their own activities that handle them.
Intent Resolution
There are two primary forms of intents you will use.
-
Explicit Intents have specified a component (via
setComponent(ComponentName)
orsetClass(Context, Class)
), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application. -
Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.
When using implicit intents, given such an arbitrary intent we need to know what to do with it. This is handled by the process of Intent resolution, which maps an Intent to an Activity
, BroadcastReceiver
, or Service
(or sometimes two or more activities/receivers) that can handle it.
The intent resolution mechanism basically revolves around matching an Intent against all of the <intent-filter> descriptions in the installed application packages. (Plus, in the case of broadcasts, any BroadcastReceiver
objects explicitly registered with registerReceiver(BroadcastReceiver, IntentFilter)
.) More details on this can be found in the documentation on the IntentFilter
class.
There are three pieces of information in the Intent that are used for resolution: the action, type, and category. Using this information, a query is done on the PackageManager
for a component that can handle the intent. The appropriate component is determined based on the intent information supplied in the AndroidManifest.xml
file as follows:
-
The action, if given, must be listed by the component as one it handles.
-
The type is retrieved from the Intent's data, if not already supplied in the Intent. Like the action, if a type is included in the intent (either explicitly or implicitly in its data), then this must be listed by the component as one it handles.
- For data that is not a
content:
URI and where no explicit type is included in the Intent, instead the scheme of the intent data (such ashttp:
ormailto:
) is considered. Again like the action, if we are matching a scheme it must be listed by the component as one it can handle. -
The categories, if supplied, must all be listed by the activity as categories it handles. That is, if you include the categories
CATEGORY_LAUNCHER
andCATEGORY_ALTERNATIVE
, then you will only resolve to components with an intent that lists both of those categories. Activities will very often need to support theCATEGORY_DEFAULT
so that they can be found byContext.startActivity()
.
For example, consider the Note Pad sample application that allows user to browse through a list of notes data and view details about individual items. Text in italics indicate places were you would replace a name with one specific to your own package.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.notepad">
<application android:icon="@drawable/app_notes"
android:label="@string/app_name">
<provider class=".NotePadProvider"
android:authorities="com.google.provider.NotePad" />
<activity class=".NotesList" android:label="@string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
</activity>
<activity class=".NoteEditor" android:label="@string/title_note">
<intent-filter android:label="@string/resolve_edit">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
</activity>
<activity class=".TitleEditor" android:label="@string/title_edit_title"
android:theme="@android:style/Theme.Dialog">
<intent-filter android:label="@string/resolve_title">
<action android:name="com.android.notepad.action.EDIT_TITLE" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.ALTERNATIVE" />
<category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
</activity>
</application>
</manifest>
The first activity, com.android.notepad.NotesList
, serves as our main entry into the app. It can do three things as described by its three intent templates:
-
<intent-filter>
<action android:name="android.intent.action.MAIN
" />
<category android:name="android.intent.category.LAUNCHER