2.App Components-Intents and Intent Filters
1. Intent and Intent Filters
An 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:
- To start an activity:
An
Activity
represents a single screen in an app. You can start a new instance of anActivity
by passing anIntent
tostartActivity()
. TheIntent
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 separateIntent
object in your activity'sonActivityResult()
callback. For more information, see the Activities guide. - To start a service:
A
Service
is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing anIntent
tostartService()
. TheIntent
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
Intent
tobindService()
. For more information, see the Services guide. - To deliver a broadcast:
A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system bootsup or the device starts charging. You can deliver a broadcast to other apps by passing an
Intent
tosendBroadcast()
,sendOrderedBroadcast()
, orsendStickyBroadcast()
2. Intent Type
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, 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.
When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the Intent
object
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
3. Building an Intent
3.1 Component name
The name of the component to start.
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 setComponent()
, setClass()
,
setClassName()
, or with the Intent
constructor.
3.2 Action
A string that specifies the generic action to perform (such as view or pick).
3.3 Data
3.4 Category
3.5 Extras
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
Intent
with putExtras()
.
3.6 Flags
3. Example explicit intent
For example, if you built a service in your app, named DownloadService
, designed to download a file from the web, you can start it with the
following code:
// Executed in an Activity, so 'this' is the Context // The fileUrl is a string URL, such as "http://www.example.com/image.png" Intent downloadIntent = new Intent(this, DownloadService.class); downloadIntent.setData(Uri.parse(fileUrl)); startService(downloadIntent);
4. Example implicit intent
An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when
your app cannot perform the action, but other apps probably can and you'd like the user to pick which app to use
// Create the text message with a string Intent sendIntent = new Intent(); //implicit intent sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type // Verify that the intent will resolve to an activity if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); }
5. Common Intents
1. Alarm Clock
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createAlarm("hh", 16, 8); } public void createAlarm(String message, int hour, int minutes) { Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_HOUR, hour) .putExtra(AlarmClock.EXTRA_MINUTES, minutes); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
2. Create a timer
3. Calendar
4. Camera
5. Contacts/People App
6. File Storage
static final int REQUEST_IMAGE_OPEN = 1; public void selectImage() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.setType("image/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); // Only the system receives the ACTION_OPEN_DOCUMENT, so no need to test. startActivityForResult(intent, REQUEST_IMAGE_OPEN); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_OPEN && resultCode == RESULT_OK) { Uri fullPhotoUri = data.getData(); // Do work with full size photo saved at fullPhotoUri ... } }
7. Maps
8. More to see Develop/App Components/Intents and intent Filters/Common Intents