original source : https://developer.android.com/guide/components/intents-filters.html
Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:
- Starting an activity
- Starting a service
- Delivering a broadcast
Activity represents a single screen in an app. You can start a new instance of an
Activity by passing an
Intent describes the activity to start and carries any necessary data.
If you want to receive a result from the activity when it finishes, call
startActivityForResult(). Your activity receives the result as a separate
Intent object in your activity’s
onActivityResult() callback. For more information, see the Activities guide.
Service is a component that performs operations in the background without a user interface. With Android 5.0 (API level 21) and later, you can start a service with
JobScheduler. For more information about
JobScheduler, see its
For versions earlier than Android 5.0 (API level 21), you can start a service by using methods of the
Service class. You can start a service to perform a one-time operation (such as downloading a file) by passing an
Intent describes the service to start and carries any necessary data.
If the service is designed with a client-server interface, you can bind to the service from another component by passing an
bindService(). For more information, see the Services guide.
A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an
There are two types of intents:
- Explicit intents specify the component to start by name (the fully-qualified class name). You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, you can start a new activity in response to a user action or start a service to download a file in the background.
- Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
Figure 1 shows how an intent is delivered to start an activity. When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the
When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the
Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.
An intent filter is an expression in an app’s manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent. Likewise, if you do notdeclare any intent filters for an activity, then it can be started only with an explicit intent.
Caution: To ensure that your app is secure, always use an explicit intent when starting a
Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you can’t be certain what service will respond to the intent, and the user can’t see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call
bindService() with an implicit intent.
Building an intent
Intent object carries information that the Android system uses to determine which component to start (such as the exact component name or component category that should receive the intent), plus information that the recipient component uses in order to properly perform the action (such as the action to take and the data to act upon).
The primary information contained in an
Intent is the following:
The name of the component to start.
This is optional, but it’s the critical piece of information that makes an intent explicit, meaning that the intent should be delivered only to the app component defined by the component name. Without a component name, the intent is implicit and the system decides which component should receive the intent based on the other intent information (such as the action, data, and category—described below). If you need to start a specific component in your app, you should specify the component name.
Note: When starting a
Service, always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
This field of the
Intent is a
ComponentName object, which you can specify using a fully qualified class name of the target component, including the package name of the app, for example,
com.example.ExampleActivity. You can set the component name with
setClassName(), or with the
A string that specifies the generic action to perform (such as view or pick).
In the case of a broadcast intent, this is the action that took place and is being reported. The action largely determines how the rest of the intent is structured—particularly the information that is contained in the data and extras.
You can specify your own actions for use by intents within your app (or for use by other apps to invoke components in your app), but you usually specify action constants defined by the
Intent class or other framework classes. Here are some common actions for starting an activity:
Use this action in an intent with
when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app.
Also known as the share intent, you should use this in an intent with
when you have some data that the user can share through another app, such as an email app or social sharing app.
Intent class reference for more constants that define generic actions. Other actions are defined elsewhere in the Android framework, such as in
Settings for actions that open specific screens in the system’s Settings app.
You can specify the action for an intent with
setAction() or with an
If you define your own actions, be sure to include your app’s package name as a prefix, as shown in the following example:
static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";
The URI (a
object) that references the data to be acted on and/or the MIME type of that data. The type of data supplied is generally dictated by the intent’s action. For example, if the action is
, the data should contain the URI of the document to edit.
When creating an intent, it’s often important to specify the type of data (its MIME type) in addition to its URI. For example, an activity that’s able to display images probably won’t be able to play an audio file, even though the URI formats could be similar. Specifying the MIME type of your data helps the Android system find the best component to receive your intent. However, the MIME type can sometimes be inferred from the URI—particularly when the data is a
content: URI. A
content: URI indicates the data is located on the device and controlled by a
ContentProvider, which makes the data MIME type visible to the system.
To set only the data URI, call
setData(). To set only the MIME type, call
setType(). If necessary, you can set both explicitly with
Caution: If you want to set both the URI and MIME type, don’t call
setType() because they each nullify the value of the other. Always use
setDataAndType() to set both URI and MIME type.
A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an intent, but most intents do not require a category. Here are some common categories:
The target activity allows itself to be started by a web browser to display data referenced by a link, such as an image or an e-mail message.
The activity is the initial activity of a task and is listed in the system’s application launcher.
Intent class description for the full list of categories.
You can specify a category with
These properties listed above (component name, action, data, and category) represent the defining characteristics of an intent. By reading these properties, the Android system is able to resolve which app component it should start. However, an intent can carry additional information that does not affect how it is resolved to an app component. An intent can also supply the following information:
Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras.
You can add extra data with various
putExtra() methods, each accepting two parameters: the key name and the value. You can also create a
Bundle object with all the extra data, then insert the
Bundle in the
For example, when creating an intent to send an email with
ACTION_SEND, you can specify the to recipient with the
EXTRA_EMAIL key, and specify the subject with the
Intent class specifies many
EXTRA_* constants for standardized data types. If you need to declare your own extra keys (for intents that your app receives), be sure to include your app’s package name as a prefix, as shown in the following example:
static final String EXTRA_GIGAWATTS = "com.example.EXTRA_GIGAWATTS";
Caution: Do not use
Serializable data when sending an intent that you expect another app to receive. If an app attempts to access data in a
Bundle object but does not have access to the parceled or serialized class, the system raises a
Flags are defined in the
class that function as metadata for the intent. The flags may instruct the Android system how to launch an activity (for example, which
the activity should belong to) and how to treat it after it’s launched (for example, whether it belongs in the list of recent activities).
For more information, see the