page.title=Common Intents page.tags="IntentFilter" @jd:body
An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an {@link android.content.Intent} object. This type of intent is called an implicit intent because it does not specify the app component to start, but instead specifies an action and provides some data with which to perform the action.
When you call {@link android.content.Context#startActivity startActivity()} or {@link android.app.Activity#startActivityForResult startActivityForResult()} and pass it an implicit intent, the system resolves the intent to an app that can handle the intent and starts its corresponding {@link android.app.Activity}. If there's more than one app that can handle the intent, the system presents the user with a dialog to pick which app to use.
This page describes several implicit intents that you can use to perform common actions, organized by the type of app that handles the intent. Each section also shows how you can create an intent filter to advertise your app's ability to perform the same action.
Caution: If there are no apps on the device that can receive the implicit intent, your app will crash when it calls {@link android.content.Context#startActivity startActivity()}. To first verify that an app exists to receive the intent, call {@link android.content.Intent#resolveActivity resolveActivity()} on your {@link android.content.Intent} object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call {@link android.content.Context#startActivity startActivity()}. If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.
If you're not familiar with how to create intents or intent filters, you should first read Intents and Intent Filters.
To open a camera app and receive the resulting photo or video, use the {@link android.provider.MediaStore#ACTION_IMAGE_CAPTURE} or {@link android.provider.MediaStore#ACTION_VIDEO_CAPTURE} action. Also specify the URI location where you'd like the camera to save the photo or video, in the {@link android.provider.MediaStore#EXTRA_OUTPUT} extra.
When the camera app successfully returns focus to your activity (your app receives the {@link android.app.Activity#onActivityResult onActivityResult()} callback), you can access the photo or video at the URI you specified with the {@link android.provider.MediaStore#EXTRA_OUTPUT} value.
Note: When you use {@link
android.provider.MediaStore#ACTION_IMAGE_CAPTURE} to capture a photo, the camera may also return a
downscaled copy (a thumbnail) of the photo in the result {@link
android.content.Intent}, saved as a {@link android.graphics.Bitmap} in an extra field named
"data"
.
Example intent:
static final int REQUEST_IMAGE_CAPTURE = 1; static final Uri mLocationForPhotos; public void capturePhoto(String targetFilename) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.withAppendedPath(mLocationForPhotos, targetFilename); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { Bitmap thumbnail = data.getParcelable("data"); // Do other work with full size photo saved in mLocationForPhotos ... } }
For more information about how to use this intent to capture a photo, including how to create an appropriate {@link android.net.Uri} for the output location, read Taking Photos Simply or Taking Videos Simply.
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
When handling this intent, your activity should check for the {@link
android.provider.MediaStore#EXTRA_OUTPUT} extra in the incoming {@link android.content.Intent},
then save the captured image or video at the location specified by that extra and call {@link
android.app.Activity#setResult(int,Intent) setResult()} with an
{@link android.content.Intent} that includes a compressed thumbnail
in an extra named "data"
.
To have the user select a contact and provide your app access to all the contact information, use the {@link android.content.Intent#ACTION_PICK} action and specify the MIME type to {@link android.provider.ContactsContract.Contacts#CONTENT_TYPE Contacts.CONTENT_TYPE}.
The result {@link android.content.Intent} delivered to your {@link
android.app.Activity#onActivityResult onActivityResult()} callback contains the
content:
URI pointing to the selected contact. The response grants
your app temporary permissions to read that contact using the Contacts Provider API even if
your app does not include the {@link android.Manifest.permission#READ_CONTACTS} permission.
Tip: If you need access to only a specific piece of contact information, such as a phone number or email address, instead see the next section about how to select specific contact data.
Example intent:
static final int REQUEST_SELECT_CONTACT = 1; public void selectContact() { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_CONTACT); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) { Uri contactUri = data.getData(); // Do something with the selected contact at contactUri ... } }
For information about how to retrieve contact details once you have the contact URI, read Retrieving Details for a Contact. Remember, when you retrieve the contact URI with the above intent, you do not need the {@link android.Manifest.permission#READ_CONTACTS} permission to read details for that contact.
To have the user select a specific piece of information from a contact, such as a phone number, email address, or other data type, use the {@link android.content.Intent#ACTION_PICK} action and specify the MIME type to one of the content types listed below, such as {@link android.provider.ContactsContract.CommonDataKinds.Phone#CONTENT_TYPE CommonDataKinds.Phone.CONTENT_TYPE} to get the contact's phone number.
If you need to retrieve only one type of data from a contact, this technique with a {@code CONTENT_TYPE} from the {@link android.provider.ContactsContract.CommonDataKinds} classes is more efficient than using the {@link android.provider.ContactsContract.Contacts#CONTENT_TYPE Contacts.CONTENT_TYPE} (as shown in the previous section) because the result provides you direct access to the desired data without requiring you to perform a more complex query to Contacts Provider.
The result {@link android.content.Intent} delivered to your {@link
android.app.Activity#onActivityResult onActivityResult()} callback contains the
content:
URI pointing to the selected contact data. The response grants
your app temporary permissions to read that contact data even if your app does
not include the {@link android.Manifest.permission#READ_CONTACTS} permission.
Or one of many other {@code CONTENT_TYPE} values under {@link android.provider.ContactsContract}.
Example intent:
static final int REQUEST_SELECT_PHONE_NUMBER = 1; public void selectContact() { // Start an activity for the user to pick a phone number from contacts Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) { // Get the URI and query the content provider for the phone number Uri contactUri = data.getData(); String[] projection = new String[]{CommonDataKinds.Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); // If the cursor returned is valid, get the phone number if (cursor != null && cursor.moveToFirst()) { int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER); String number = cursor.getString(numberIndex); // Do something with the phone number ... } } }
To display the details for a known contact, use the {@link android.content.Intent#ACTION_VIEW} action and specify the contact with a {@code content:} URI as the intent data.
There are primarily two ways to initially retrieve the contact's URI:
Example intent:
public void viewContact(Uri contactUri) { Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
To edit a known contact, use the {@link android.content.Intent#ACTION_EDIT} action, specify the contact with a {@code content:} URI as the intent data, and include any known contact information in extras specified by constants in {@link android.provider.ContactsContract.Intents.Insert}.
There are primarily two ways to initially retrieve the contact URI:
Example intent:
public void editContact(Uri contactUri, String email) { Intent intent = new Intent(Intent.ACTION_EDIT); intent.setDataAndType(contactUri, Contacts.CONTENT_TYPE); intent.putExtra(Intents.Insert.EMAIL, email); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
For more information about how to edit a contact, read Modifying Contacts Using Intents.
To insert a new contact, use the {@link android.content.Intent#ACTION_INSERT} action, specify {@link android.provider.ContactsContract.Contacts#CONTENT_TYPE Contacts.CONTENT_TYPE} as the MIME type, and include any known contact information in extras specified by constants in {@link android.provider.ContactsContract.Intents.Insert}.
Example intent:
public void insertContact(String name, String email) { Intent intent = new Intent(Intent.ACTION_INSERT); intent.setType(Contacts.CONTENT_TYPE); intent.putExtra(Intents.Insert.NAME, name); intent.putExtra(Intents.Insert.EMAIL, email); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
For more information about how to insert a contact, read Modifying Contacts Using Intents.
To compose an email, use one of the below actions based on whether you'll include attachments, and include email details such as the recipient and subject using the extra keys listed below.
"*/*"
Example intent:
public void composeEmail(String[] addresses, String subject, Uri attachment) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("*/*"); intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_STREAM, attachment); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
If you want to ensure that your intent is handled only by an email app (and not other text messaging or social apps), then use the {@link android.content.Intent#ACTION_SENDTO} action and include the {@code "mailto:"} data scheme. For example:
public void composeEmail(String[] addresses, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this intent.putExtra(Intent.EXTRA_EMAIL, addresses); intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SEND" /> <data android:type="*/*" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SENDTO" /> <data android:scheme="mailto" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
To request that the user select a file such as a document or photo and return a reference to your app, use the {@link android.content.Intent#ACTION_GET_CONTENT} action and specify your desired MIME type. The file reference returned to your app is transient to your activity's current lifecycle, so if you want to access it later you must import a copy that you can read later. This intent also allows the user to create a new file in the process (for example, instead of selecting an existing photo, the user can capture a new photo with the camera).
The result intent delivered to your {@link android.app.Activity#onActivityResult onActivityResult()} method includes data with a URI pointing to the file. The URI could be anything, such as an {@code http:} URI, {@code file:} URI, or {@code content:} URI. However, if you'd like to restrict selectable files to only those that are accessible from a content provider (a {@code content:} URI) and that are available as a file stream with {@link android.content.ContentResolver#openFileDescriptor openFileDescriptor()}, you should add the {@link android.content.Intent#CATEGORY_OPENABLE} category to your intent.
On Android 4.3 (API level 18) and higher, you can also allow the user to select multiple files by adding {@link android.content.Intent#EXTRA_ALLOW_MULTIPLE} to the intent, set to {@code true}. You can then access each of the selected files in a {@link android.content.ClipData} object returned by {@link android.content.Intent#getClipData()}.
Example intent to get a photo:
static final int REQUEST_IMAGE_GET = 1; public void selectImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_GET); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) { Bitmap thumbnail = data.getParcelable("data"); Uri fullPhotoUri = data.getData(); // Do work with photo saved at fullPhotoUri ... } }
Example intent filter to return a photo:
<activity ...> <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <data android:type="image/*" /> <category android:name="android.intent.category.DEFAULT" /> <!-- The OPENABLE category declares that the returned file is accessible from a content provider that supports {@link android.provider.OpenableColumns} and {@link android.content.ContentResolver#openFileDescriptor ContentResolver.openFileDescriptor()} --> <category android:name="android.intent.category.OPENABLE" /> </intent-filter> </activity>
Instead of retrieving a copy of a file that you must import to your app (by using the {@link android.content.Intent#ACTION_GET_CONTENT} action), when running on Android 4.4 or higher, you can instead request to open a file that's managed by another app by using the {@link android.content.Intent#ACTION_OPEN_DOCUMENT} action and specifying a MIME type. To also allow the user to instead create a new document that your app can write to, use the {@link android.content.Intent#ACTION_CREATE_DOCUMENT} action instead. For example, instead of selecting from existing PDF documents, the {@link android.content.Intent#ACTION_CREATE_DOCUMENT} intent allows users to select where they'd like to create a new document (within another app that manages the document's storage)—your app then receives the URI location of where it can write the new document.
Whereas the intent delivered to your {@link android.app.Activity#onActivityResult onActivityResult()} method from the {@link android.content.Intent#ACTION_GET_CONTENT} action may return a URI of any type, the result intent from {@link android.content.Intent#ACTION_OPEN_DOCUMENT} and {@link android.content.Intent#ACTION_CREATE_DOCUMENT} always specify the chosen file as a {@code content:} URI that's backed by a {@link android.provider.DocumentsProvider}. You can open the file with {@link android.content.ContentResolver#openFileDescriptor openFileDescriptor()} and query its details using columns from {@link android.provider.DocumentsContract.Document}.
The returned URI grants your app long-term read access to the file (also possibly with write access). So the {@link android.content.Intent#ACTION_OPEN_DOCUMENT} action is particularly useful (instead of using {@link android.content.Intent#ACTION_GET_CONTENT}) when you want to read an existing file without making a copy into your app, or when you want to open and edit a file in place.
You can also allow the user to select multiple files by adding {@link android.content.Intent#EXTRA_ALLOW_MULTIPLE} to the intent, set to {@code true}. If the user selects just one item, then you can retrieve the item from {@link android.content.Intent#getData()}. If the user selects more than one item, then {@link android.content.Intent#getData()} returns null and you must instead retrieve each item from a {@link android.content.ClipData} object that is returned by {@link android.content.Intent#getClipData()}.
Note: Your intent must specify a MIME type and must declare the {@link android.content.Intent#CATEGORY_OPENABLE} category. If appropriate, you can specify more than one MIME type by adding an array of MIME types with the {@link android.content.Intent#EXTRA_MIME_TYPES} extra—if you do so, you must set the primary MIME type in {@link android.content.Intent#setType setType()} to {@code "*/*"}.
Example intent to get a photo:
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 ... } }
Third party apps cannot actually respond to an intent with the {@link android.content.Intent#ACTION_OPEN_DOCUMENT} action. Instead, the system receives this intent and displays all the files available from various apps in a unified user interface.
To provide your app's files in this UI and allow other apps to open them, you must implement a {@link android.provider.DocumentsProvider} and include an intent filter for {@link android.provider.DocumentsContract#PROVIDER_INTERFACE} ({@code "android.content.action.DOCUMENTS_PROVIDER"}). For example:
<provider ... android:grantUriPermissions="true" android:exported="true" android:permission="android.permission.MANAGE_DOCUMENTS"> <intent-filter> <action android:name="android.content.action.DOCUMENTS_PROVIDER" /> </intent-filter> </provider>
For more information about how to make the files managed by your app openable from other apps, read the Storage Access Framework guide.
To open a map, use the {@link android.content.Intent#ACTION_VIEW} action and specify the location information in the intent data with one of the schemes defined below.
geo:latitude,longitude
Example: "geo:47.6,-122.3"
geo:latitude,longitude?z=zoom
Example: "geo:47.6,-122.3?z=11"
geo:0,0?q=lat,lng(label)
Example: "geo:0,0?q=34.99,-106.61(Treasure)"
geo:0,0?q=my+street+address
Example: "geo:0,0?q=1600+amphitheatre+parkway+ca"
Example intent:
public void showMap(Uri geoLocation) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:scheme="geo" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
To play a music file, use the {@link android.content.Intent#ACTION_VIEW} action and specify the URI location of the file in the intent data.
"audio/*"
"application/ogg"
"application/x-ogg"
"application/itunes"
Example intent:
public void playMedia(Uri file) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(file); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:type="audio/*" /> <data android:type="application/ogg" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
To open the phone app and dial a phone number, use the {@link android.content.Intent#ACTION_DIAL} action and specify a phone number using the URI scheme defined below. When the phone app opens, it displays the phone number but the user must press the Call button to begin the phone call.
Valid telephone numbers are those defined in the IETF RFC 3966. Valid examples include the following:
tel:2125551212
tel:(212) 555 1212
The Phone's dialer is good at normalizing schemes, such as telephone numbers. So the scheme described isn't strictly required in the {@link android.net.Uri#parse(String) Uri.parse()} method. However, if you have not tried a scheme or are unsure whether it can be handled, use the {@link android.net.Uri#fromParts Uri.fromParts()} method instead.
Example intent:
public void dialPhoneNumber(String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + phoneNumber)); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
To open a screen in the system settings when your app requires the user to change something, use one of the following intent actions to open the settings screen respective to the action name.
See the {@link android.provider.Settings} documentation for additional settings screens that are available.
Example intent:
public void openWifiSettings() { Intent intent = new Intent(Intent.ACTION_WIFI_SETTINGS); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
To initiate an SMS or MMS text message, use one of the intent actions below and specify message details such as the phone number, subject, and message body using the extra keys listed below.
Each of these schemes are handled the same.
"text/plain"
)
"image/*"
"video/*"
"subject"
"sms_body"
Example intent:
public void composeMmsMessage(String message, Uri attachment) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(HTTP.PLAIN_TEXT_TYPE); intent.putExtra("sms_body", message); intent.putExtra(Intent.EXTRA_STREAM, attachment); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
If you want to ensure that your intent is handled only by a text messaging app (and not other email or social apps), then use the {@link android.content.Intent#ACTION_SENDTO} action and include the {@code "smsto:"} data scheme. For example:
public void composeMmsMessage(String message, Uri attachment) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setData(Uri.parse("smsto:")); // This ensures only SMS apps respond intent.putExtra("sms_body", message); intent.putExtra(Intent.EXTRA_STREAM, attachment); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SEND" /> <data android:type="text/plain" /> <data android:type="image/*" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Note: If you're developing an SMS/MMS messaging app, you must implement intent filters for several additional actions in order to be available as the default SMS app on Android 4.4 and higher. For more information, see the documentation at {@link android.provider.Telephony}.
To open a web page, use the {@link android.content.Intent#ACTION_VIEW} action and specify the web URL in the intent data.
"text/plain"
)
"text/html"
"application/xhtml+xml"
"application/vnd.wap.xhtml+xml"
Example intent:
public void openWebPage(String url) { Uri webpage = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, webpage); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.VIEW" /> <!-- Include the host attribute if you want your app to respond only to URLs with your app's domain. --> <data android:scheme="http" android:host="www.example.com" /> <category android:name="android.intent.category.DEFAULT" /> <!-- The BROWSABLE category is required to get links from web pages. --> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity>
Tip: If your Android app provides functionality similar to your web site, include an intent filter for URLs that point to your web site. Then, if users have your app installed, links from emails or other web pages pointing to your web site open your Android app instead of your web page.
To initiate a web search, use the {@link android.content.Intent#ACTION_WEB_SEARCH} action and specify the search string in the {@link android.app.SearchManager#QUERY SearchManager.QUERY} extra.
Example intent:
public void searchWeb(String query) { Intent intent = new Intent(Intent.ACTION_SEARCH); intent.putExtra(SearchManager.QUERY, query); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }