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 Intent
object. This type of intent is called an implicitintent 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 startActivity()
or startActivityForResult()
and pass it an implicit intent, the system resolves the intent to an app that can handle the intent and starts its corresponding 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 callsstartActivity()
. To first verify that an app exists to receive the intent, call resolveActivity()
on your 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 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 learn how to fire the intents listed on this page from your development host, see Verify Intents with the Android Debug Bridge.
Google Now
Google Now fires some of the intents listed on this page in response to voice commands. For more information, see Intents Fired by Google Now.
Alarm Clock
Create an alarm
Note: Only the hour, minutes, and message extras are available in Android 2.3 (API level 9) and higher. The other extras were added in later versions of the platform.To create a new alarm, use the ACTION_SET_ALARM
action and specify alarm details such as the time and message using extras defined below.
-
Action
-
ACTION_SET_ALARM
Data URI
- None MIME Type
- None Extras
-
- The hour for the alarm.
- The minutes for the alarm.
- A custom message to identify the alarm.
-
An
ArrayList
including each week day on which this alarm should be repeated. Each day must be declared with an integer from theCalendar
class such asMONDAY
.For a one-time alarm, do not specify this extra.
-
A
content:
URI specifying a ringtone to use with the alarm, orVALUE_RINGTONE_SILENT
for no ringtone.To use the default ringtone, do not specify this extra.
- A boolean specifying whether to vibrate for this alarm.
- A boolean specifying whether the responding app should skip its UI when setting the alarm. If true, the app should bypass any confirmation UI and simply set the specified alarm.
EXTRA_HOUR
EXTRA_MINUTES
EXTRA_MESSAGE
EXTRA_DAYS
EXTRA_RINGTONE
EXTRA_VIBRATE
EXTRA_SKIP_UI
Example intent:
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); } }
In order to invoke the ACTION_SET_ALARM
intent, your app must have the SET_ALARM
permission:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SET_ALARM" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Create a timer
To create a countdown timer, use the ACTION_SET_TIMER
action and specify timer details such as the duration using extras defined below.
Note: This intent was added in Android 4.4 (API level 19).
-
Action
-
ACTION_SET_TIMER
Data URI
- None MIME Type
- None Extras
-
- The length of the timer in seconds.
- A custom message to identify the timer.
- A boolean specifying whether the responding app should skip its UI when setting the timer. If true, the app should bypass any confirmation UI and simply start the specified timer.
EXTRA_LENGTH
EXTRA_MESSAGE
EXTRA_SKIP_UI
Example intent:
public void startTimer(String message, int seconds) { Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER) .putExtra(AlarmClock.EXTRA_MESSAGE, message) .putExtra(AlarmClock.EXTRA_LENGTH, seconds) .putExtra(AlarmClock.EXTRA_SKIP_UI, true); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
In order to invoke the ACTION_SET_TIMER
intent, your app must have the SET_ALARM
permission:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SET_TIMER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Show all alarms
To show the list of alarms, use the ACTION_SHOW_ALARMS
action.
Although not many apps will invoke this intent (it's primarily used by system apps), any app that behaves as an alarm clock should implement this intent filter and respond by showing the list of current alarms.
Note: This intent was added in Android 4.4 (API level 19).
-
Action
-
ACTION_SHOW_ALARMS
Data URI
- None MIME Type
- None
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SHOW_ALARMS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Calendar
Add a calendar event
To add a new event to the user's calendar, use the ACTION_INSERT
action and specify the data URI withEvents.CONTENT_URI
. You can then specify various event details using extras defined below.
-
Action
-
ACTION_INSERT
Data URI
-
Events.CONTENT_URI
MIME Type
-
"vnd.android.cursor.dir/event"
Extras
-
- A boolean specifying whether this is an all-day event.
- The start time of the event (milliseconds since epoch).
- The end time of the event (milliseconds since epoch).
- The event title.
- The event description.
- The event location.
- A comma-separated list of email addresses that specify the invitees.
EXTRA_EVENT_ALL_DAY
EXTRA_EVENT_BEGIN_TIME
EXTRA_EVENT_END_TIME
TITLE
DESCRIPTION
EVENT_LOCATION
EXTRA_EMAIL
Many more event details can be specified using the constants defined in the
CalendarContract.EventsColumns
class.
Example intent:
public void addEvent(String title, String location, Calendar begin, Calendar end) { Intent intent = new Intent(Intent.ACTION_INSERT) .setData(Events.CONTENT_URI) .putExtra(Events.TITLE, title) .putExtra(Events.EVENT_LOCATION, location) .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin) .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.INSERT" /> <data android:mimeType="vnd.android.cursor.dir/event" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Camera
Capture a picture or video and return it
To open a camera app and receive the resulting photo or video, use the ACTION_IMAGE_CAPTURE
orACTION_VIDEO_CAPTURE
action. Also specify the URI location where you'd like the camera to save the photo or video, in the EXTRA_OUTPUT
extra.
-
Action
-
ACTION_IMAGE_CAPTURE
or
ACTION_VIDEO_CAPTURE
Data URI Scheme
- None MIME Type
- None Extras
-
-
The URI location where the camera app should save the photo or video file (as a
Uri
object).
EXTRA_OUTPUT
-
The URI location where the camera app should save the photo or video file (as a
When the camera app successfully returns focus to your activity (your app receives the onActivityResult()
callback), you can access the photo or video at the URI you specified with the EXTRA_OUTPUT
value.
Note: When you use ACTION_IMAGE_CAPTURE
to capture a photo, the camera may also return a downscaled copy (a thumbnail) of the photo in the result Intent
, saved as a 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 appropriateUri
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 EXTRA_OUTPUT
extra in the incoming Intent
, then save the captured image or video at the location specified by that extra and call setResult()
with an Intent
that includes a compressed thumbnail in an extra named "data"
.
Start a camera app in still image mode
ActionTo open a camera app in still image mode, use theINTENT_ACTION_STILL_IMAGE_CAMERA
action.
-
INTENT_ACTION_STILL_IMAGE_CAMERA
Data URI Scheme
- None MIME Type
- None Extras
- None
Example intent:
public void capturePhoto() { Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.STILL_IMAGE_CAMERA" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Start a camera app in video mode
ActionTo open a camera app in video mode, use theINTENT_ACTION_VIDEO_CAMERA
action.
-
INTENT_ACTION_VIDEO_CAMERA
Data URI Scheme
- None MIME Type
- None Extras
- None
Example intent:
public void capturePhoto() { Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.VIDEO_CAMERA" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Contacts/People App
Select a contact
To have the user select a contact and provide your app access to all the contact information, use theACTION_PICK
action and specify the MIME type to Contacts.CONTENT_TYPE
.
The result Intent
delivered to your 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 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.
-
Action
-
ACTION_PICK
Data URI Scheme
- None MIME Type
-
Contacts.CONTENT_TYPE
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 theREAD_CONTACTS
permission to read details for that contact.
Select specific contact data
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 ACTION_PICK
action and specify the MIME type to one of the content types listed below, such as 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 CONTENT_TYPE
from theContactsContract.CommonDataKinds
classes is more efficient than using the 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 Intent
delivered to your 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 READ_CONTACTS
permission.
-
Action
-
ACTION_PICK
Data URI Scheme
- None MIME Type
-
- Pick from contacts with a phone number.
- Pick from contacts with an email address.
- Pick from contacts with a postal address.
CommonDataKinds.Phone.CONTENT_TYPE
CommonDataKinds.Email.CONTENT_TYPE
CommonDataKinds.StructuredPostal.CONTENT_TYPE
Or one of many other
CONTENT_TYPE
values underContactsContract
.
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 ... } } }
View a contact
To display the details for a known contact, use the ACTION_VIEW
action and specify the contact with acontent:
URI as the intent data.
There are primarily two ways to initially retrieve the contact's URI:
- Use the contact URI returned by the
ACTION_PICK
, shown in the previous section (this approach does not require any app permissions). - Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the
READ_CONTACTS
permission).
-
Action
-
ACTION_VIEW
Data URI Scheme
-
content:<URI>
MIME Type
- None. The type is inferred from contact URI.
Example intent:
public void viewContact(Uri contactUri) { Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Edit an existing contact
To edit a known contact, use the ACTION_EDIT
action, specify the contact with a content:
URI as the intent data, and include any known contact information in extras specified by constants inContactsContract.Intents.Insert
.
There are primarily two ways to initially retrieve the contact URI:
- Use the contact URI returned by the
ACTION_PICK
, shown in the previous section (this approach does not require any app permissions). - Access the list of all contacts directly, as described in Retrieving a List of Contacts (this approach requires the
READ_CONTACTS
permission).
-
Action
-
ACTION_EDIT
Data URI Scheme
-
content:<URI>
MIME Type
- The type is inferred from contact URI. Extras
-
One or more of the extras defined in
ContactsContract.Intents.Insert
so you can populate fields of the contact details.
Example intent:
public void editContact(Uri contactUri, String email) { Intent intent = new Intent(Intent.ACTION_EDIT); intent.setData(contactUri); 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.
Insert a contact
To insert a new contact, use the ACTION_INSERT
action, specify Contacts.CONTENT_TYPE
as the MIME type, and include any known contact information in extras specified by constants inContactsContract.Intents.Insert
.
-
Action
-
ACTION_INSERT
Data URI Scheme
- None MIME Type
-
Contacts.CONTENT_TYPE
Extras
-
One or more of the extras defined in
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.
Compose an email with optional attachments
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.
-
Action
-
ACTION_SENDTO
(for no attachment) or
ACTION_SEND
(for one attachment) or
ACTION_SEND_MULTIPLE
(for multiple attachments)
Data URI Scheme
- None MIME Type
-
"text/plain"
"*/*"
Extras
-
- A string array of all "To" recipient email addresses.
- A string array of all "CC" recipient email addresses.
- A string array of all "BCC" recipient email addresses.
- A string with the email subject.
- A string with the body of the email.
-
A
Uri
pointing to the attachment. If using theACTION_SEND_MULTIPLE
action, this should instead be anArrayList
containing multipleUri
objects.
Intent.EXTRA_EMAIL
Intent.EXTRA_CC
Intent.EXTRA_BCC
Intent.EXTRA_SUBJECT
Intent.EXTRA_TEXT
Intent.EXTRA_STREAM
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 ACTION_SENDTO
action and include the "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>
File Storage
Retrieve a specific type of file
To request that the user select a file such as a document or photo and return a reference to your app, use theACTION_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 onActivityResult()
method includes data with a URI pointing to the file. The URI could be anything, such as an http:
URI, file:
URI, or content:
URI. However, if you'd like to restrict selectable files to only those that are accessible from a content provider (a content:
URI) and that are available as a file stream with openFileDescriptor()
, you should add the 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 addingEXTRA_ALLOW_MULTIPLE
to the intent, set to true
. You can then access each of the selected files in aClipData
object returned by getClipData()
.
-
Action
-
ACTION_GET_CONTENT
Data URI Scheme
- None MIME Type
- The MIME type corresponding to the file type the user should select. Extras
-
- A boolean declaring whether the user can select more than one file at a time.
- A boolean that declares whether the returned file must be available directly from the device, rather than requiring a download from a remote service.
EXTRA_ALLOW_MULTIPLE
EXTRA_LOCAL_ONLY
Category (optional)
-
-
To return only "openable" files that can be represented as a file stream with
openFileDescriptor()
.
CATEGORY_OPENABLE
-
To return only "openable" files that can be represented as a file stream with
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 supportsOpenableColumns
andContentResolver.openFileDescriptor()
--> <category android:name="android.intent.category.OPENABLE" /> </intent-filter> </activity>
Open a specific type of file
Instead of retrieving a copy of a file that you must import to your app (by using the 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 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 ACTION_CREATE_DOCUMENT
action instead. For example, instead of selecting from existing PDF documents, the 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 onActivityResult()
method from the ACTION_GET_CONTENT
action may return a URI of any type, the result intent from ACTION_OPEN_DOCUMENT
and ACTION_CREATE_DOCUMENT
always specify the chosen file as a content:
URI that's backed by a DocumentsProvider
. You can open the file withopenFileDescriptor()
and query its details using columns from DocumentsContract.Document
.
The returned URI grants your app long-term read access to the file (also possibly with write access). So theACTION_OPEN_DOCUMENT
action is particularly useful (instead of using 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 EXTRA_ALLOW_MULTIPLE
to the intent, set to true
. If the user selects just one item, then you can retrieve the item from getData()
. If the user selects more than one item, then getData()
returns null and you must instead retrieve each item from a ClipData
object that is returned by getClipData()
.
Note: Your intent must specify a MIME type and must declare the CATEGORY_OPENABLE
category. If appropriate, you can specify more than one MIME type by adding an array of MIME types with theEXTRA_MIME_TYPES
extra—if you do so, you must set the primary MIME type in setType()
to "*/*"
.
-
Action
-
ACTION_OPEN_DOCUMENT
or
ACTION_CREATE_DOCUMENT
Data URI Scheme
- None MIME Type
- The MIME type corresponding to the file type the user should select. Extras
-
-
An array of MIME types corresponding to the types of files your app is requesting. When you use this extra, you must set the primary MIME type in
setType()
to"*/*"
. - A boolean that declares whether the user can select more than one file at a time.
-
For use with
ACTION_CREATE_DOCUMENT
to specify an initial file name. - A boolean that declares whether the returned file must be available directly from the device, rather than requiring a download from a remote service.
EXTRA_MIME_TYPES
EXTRA_ALLOW_MULTIPLE
EXTRA_TITLE
EXTRA_LOCAL_ONLY
Category
-
An array of MIME types corresponding to the types of files your app is requesting. When you use this extra, you must set the primary MIME type in
-
-
To return only "openable" files that can be represented as a file stream with
openFileDescriptor()
.
CATEGORY_OPENABLE
-
To return only "openable" files that can be represented as a file stream with
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 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 aDocumentsProvider
and include an intent filter for PROVIDER_INTERFACE
("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 theStorage Access Framework guide.
Local Actions
Call a car
Note: Apps must ask for confirmation from the user before completing the action.To call a taxi, use the ACTION_RESERVE_TAXI_RESERVATION
action.
-
Action
-
ACTION_RESERVE_TAXI_RESERVATION
Data URI
- None MIME Type
- None Extras
- None
Example intent:
public void callCar() { Intent intent = new Intent(ReserveIntents.ACTION_RESERVE_TAXI_RESERVATION); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="com.google.android.gms.actions.RESERVE_TAXI_RESERVATION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Maps
Show a location on a map
To open a map, use the ACTION_VIEW
action and specify the location information in the intent data with one of the schemes defined below.
-
Action
-
ACTION_VIEW
Data URI Scheme
-
-
Show the map at the given longitude and latitude.
Example:
"geo:47.6,-122.3"
-
Show the map at the given longitude and latitude at a certain zoom level. A zoom level of 1 shows the whole Earth, centered at the given
lat,
lng. The highest (closest) zoom level is 23.
Example:
"geo:47.6,-122.3?z=11"
-
Show the map at the given longitude and latitude with a string label.
Example:
"geo:0,0?q=34.99,-106.61(Treasure)"
-
Show the location for "my street address" (may be a specific address or location query).
Example:
"geo:0,0?q=1600+Amphitheatre+Parkway%2C+CA"
Note: All strings passed in the
geo
URI must be encoded. For example, the string1st & Pike, Seattle
should become1st%20%26%20Pike%2C%20Seattle
. Spaces in the string can be encoded with%20
or replaced with the plus sign (+
).
geo:latitude,longitude
geo:latitude,longitude?z=zoom
geo:0,0?q=lat,lng(label)
geo:0,0?q=my+street+address
MIME Type
-
Show the map at the given longitude and latitude.
- None
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>
Music or Video
Play a media file
To play a music file, use the ACTION_VIEW
action and specify the URI location of the file in the intent data.
-
Action
-
ACTION_VIEW
Data URI Scheme
-
file:<URI>
content:<URI>
http:<URL>
MIME Type
-
"audio/*"
"application/ogg"
"application/x-ogg"
"application/itunes"
Or any other that your app may require.
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>
Play music based on a search query
This intent should include the EXTRA_MEDIA_FOCUS
string extra, which specifies the inteded search mode. For example, the search mode can specify whether the search is for an artist name or song name.To play music based on a search query, use theINTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
intent. An app may fire this intent in response to the user's voice command to play music. The receiving app for this intent performs a search within its inventory to match existing content to the given query and starts playing that content.
-
Action
-
INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
Data URI Scheme
- None MIME Type
- None Extras
-
-
Indicates the search mode (whether the user is looking for a particular artist, album, song, or playlist). Most search modes take additional extras. For example, if the user is interested in listening to a particular song, the intent might have three additional extras: the song title, the artist, and the album. This intent supports the following search modes for each value of
EXTRA_MEDIA_FOCUS
:-
Play any music. The receiving app should play some music based on a smart choice, such as the last playlist the user listened to.
Additional extras:
QUERY
(required) - An empty string. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
-
Play a particular song, album or genre from an unstructured search query. Apps may generate an intent with this search mode when they can't identify the type of content the user wants to listen to. Apps should use more specific search modes when possible.
Additional extras:
QUERY
(required) - A string that contains any combination of: the artist, the album, the song name, or the genre.
-
Play music of a particular genre.
Additional extras:
"android.intent.extra.genre"
(required) - The genre.QUERY
(required) - The genre. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
-
Play music from a particular artist.
Additional extras:
EXTRA_MEDIA_ARTIST
(required) - The artist."android.intent.extra.genre"
- The genre.QUERY
(required) - A string that contains any combination of the artist or the genre. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
-
Play music from a particular album.
Additional extras:
EXTRA_MEDIA_ALBUM
(required) - The album.EXTRA_MEDIA_ARTIST
- The artist."android.intent.extra.genre"
- The genre.QUERY
(required) - A string that contains any combination of the album or the artist. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
-
Play a particular song.
Additional extras:
EXTRA_MEDIA_ALBUM
- The album.EXTRA_MEDIA_ARTIST
- The artist."android.intent.extra.genre"
- The genre.EXTRA_MEDIA_TITLE
(required) - The song name.QUERY
(required) - A string that contains any combination of: the album, the artist, the genre, or the title. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
-
Play a particular playlist or a playlist that matches some criteria specified by additional extras.
Additional extras:
EXTRA_MEDIA_ALBUM
- The album.EXTRA_MEDIA_ARTIST
- The artist."android.intent.extra.genre"
- The genre."android.intent.extra.playlist"
- The playlist.EXTRA_MEDIA_TITLE
- The song name that the playlist is based on.QUERY
(required) - A string that contains any combination of: the album, the artist, the genre, the playlist, or the title. This extra is always provided for backward compatibility: existing apps that do not know about search modes can process this intent as an unstructured search.
Any -
"vnd.android.cursor.item/*"
Unstructured -
"vnd.android.cursor.item/*"
Genre -
Audio.Genres.ENTRY_CONTENT_TYPE
Artist -
Audio.Artists.ENTRY_CONTENT_TYPE
Album -
Audio.Albums.ENTRY_CONTENT_TYPE
Song -
"vnd.android.cursor.item/audio"
Playlist -
Audio.Playlists.ENTRY_CONTENT_TYPE
-
MediaStore.EXTRA_MEDIA_FOCUS
(required) -
Example intent:
If the user wants to listen to music from a particular artist, a search app may generate the following intent:
public void playSearchArtist(String artist) { Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH); intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE); intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist); intent.putExtra(SearchManager.QUERY, artist); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.media.action.MEDIA_PLAY_FROM_SEARCH" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
When handling this intent, your activity should check the value of the EXTRA_MEDIA_FOCUS
extra in the incomingIntent
to determine the search mode. Once your activity has identified the search mode, it should read the values of the additional extras for that particular search mode. With this information your app can then perform the search within its inventory to play the content that matches the search query. For example:
protected void onCreate(Bundle savedInstanceState) { ... Intent intent = this.getIntent(); if (intent.getAction().compareTo(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) == 0) { String mediaFocus = intent.getStringExtra(MediaStore.EXTRA_MEDIA_FOCUS); String query = intent.getStringExtra(SearchManager.QUERY); // Some of these extras may not be available depending on the search mode String album = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ALBUM); String artist = intent.getStringExtra(MediaStore.EXTRA_MEDIA_ARTIST); String genre = intent.getStringExtra("android.intent.extra.genre"); String playlist = intent.getStringExtra("android.intent.extra.playlist"); String title = intent.getStringExtra(MediaStore.EXTRA_MEDIA_TITLE); // Determine the search mode and use the corresponding extras if (mediaFocus == null) { // 'Unstructured' search mode (backward compatible) playUnstructuredSearch(query); } else if (mediaFocus.compareTo("vnd.android.cursor.item/*") == 0) { if (query.isEmpty()) { // 'Any' search mode playResumeLastPlaylist(); } else { // 'Unstructured' search mode playUnstructuredSearch(query); } } else if (mediaFocus.compareTo(MediaStore.Audio.Genres.ENTRY_CONTENT_TYPE) == 0) { // 'Genre' search mode playGenre(genre); } else if (mediaFocus.compareTo(MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE) == 0) { // 'Artist' search mode playArtist(artist, genre); } else if (mediaFocus.compareTo(MediaStore.Audio.Albums.ENTRY_CONTENT_TYPE) == 0) { // 'Album' search mode playAlbum(album, artist); } else if (mediaFocus.compareTo("vnd.android.cursor.item/audio") == 0) { // 'Song' search mode playSong(album, artist, genre, title); } else if (mediaFocus.compareTo(MediaStore.Audio.Playlists.ENTRY_CONTENT_TYPE) == 0) { // 'Playlist' search mode playPlaylist(album, artist, genre, playlist, title); } } }
New Note
Create a note
To create a new note, use the ACTION_CREATE_NOTE
action and specify note details such as the subject and text using extras defined below.
Note: Apps must ask for confirmation from the user before completing the action.
Category | Details and Examples | Action Name |
---|---|---|
Alarm |
| AlarmClock.ACTION_SET_ALARM |
| AlarmClock.ACTION_SET_TIMER | |
Communication |
| Intent.ACTION_CALL |
Local |
| ReserveIntents |
Media |
| MediaStore |
| MediaStore | |
| MediaStore | |
Search |
| "com.google.android.gms.actions |
Web browser |
| Intent.ACTION_VIEW |
-
Action
-
ACTION_CREATE_NOTE
Data URI Scheme
- None MIME Type
-
PLAIN_TEXT_TYPE
- "*/*" Extras
-
- A string indicating the title or subject of the note.
- A string indicating the text of the note.
EXTRA_NAME
EXTRA_TEXT
Example intent:
public void createNote(String subject, String text) { Intent intent = new Intent(NoteIntents.ACTION_CREATE_NOTE) .putExtra(NoteIntents.EXTRA_NAME, subject) .putExtra(NoteIntents.EXTRA_TEXT, text); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Example intent filter:
<activity ...> <intent-filter> <action android:name="com.google.android.gms.actions.CREATE_NOTE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType=”*/*”> </intent-filter> </activity>
Phone
Initiate a phone call
To open the phone app and dial a phone number, use the
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.
TheACTION_CALL
action requires that you add theCALL_PHONE
permission to your manifest file:To place a phone call directly, use theACTION_CALL
action and specify a phone number using the URI scheme defined below. When the phone app opens, it begins the phone call; the user does not need to press theCall button.<uses-permission android:name="android.permission.CALL_PHONE" />
-
Action
-
ACTION_DIAL
- Opens the dialer or phone app.ACTION_CALL
- Places a phone call (requires theCALL_PHONE
permission)
Data URI Scheme
-
tel:<phone-number>
voicemail:<phone-number>
MIME Type
- None
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
Uri.parse()
method. However, if you have not tried a scheme or are unsure whether it can be handled, use theUri.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); } }
Search
Search using a specific app
Voice search in your app
To support search within the context of your app, declare an intent filter in your app with the
SEARCH_ACTION
action, as shown in the example intent filter below.-
Action
-
- Support search queries from Google Now.
"com.google.android.gms.actions.SEARCH_ACTION"
Extras
-
-
A string that contains the search query.
QUERY
-
A string that contains the search query.
Example intent filter:
<activity android:name=".SearchActivity"> <intent-filter> <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
Perform a web search
To initiate a web search, use the
ACTION_WEB_SEARCH
action and specify the search string in theSearchManager.QUERY
extra.-
Action
-
ACTION_WEB_SEARCH
Data URI Scheme
- None MIME Type
- None Extras
-
- The search string.
SearchManager.QUERY
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); } }
Settings
Open a specific section of Settings
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.
-
Action
-
ACTION_SETTINGS
ACTION_WIRELESS_SETTINGS
ACTION_AIRPLANE_MODE_SETTINGS
ACTION_WIFI_SETTINGS
ACTION_APN_SETTINGS
ACTION_BLUETOOTH_SETTINGS
ACTION_DATE_SETTINGS
ACTION_LOCALE_SETTINGS
ACTION_INPUT_METHOD_SETTINGS
ACTION_DISPLAY_SETTINGS
ACTION_SECURITY_SETTINGS
ACTION_LOCATION_SOURCE_SETTINGS
ACTION_INTERNAL_STORAGE_SETTINGS
ACTION_MEMORY_CARD_SETTINGS
See the
Settings
documentation for additional settings screens that are available.
Data URI Scheme
- None MIME Type
- None
Example intent:
public void openWifiSettings() { Intent intent = new Intent(Intent.ACTION_WIFI_SETTINGS); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Text Messaging
Compose an SMS/MMS message with attachment
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.
-
Action
-
ACTION_SENDTO
or
ACTION_SEND
or
ACTION_SEND_MULTIPLE
Data URI Scheme
-
sms:<phone_number>
smsto:<phone_number>
mms:<phone_number>
mmsto:<phone_number>
Each of these schemes are handled the same.
MIME Type
-
"text/plain"
"image/*"
"video/*"
Extras
-
- A string for the message subject (usually for MMS only).
- A string for the text message.
-
A
Uri
pointing to the image or video to attach. If using theACTION_SEND_MULTIPLE
action, this extra should be anArrayList
ofUri
s pointing to the images/videos to attach.
"subject"
"sms_body"
EXTRA_STREAM
Example intent:
public void composeMmsMessage(String message, Uri attachment) { Intent intent = new Intent(Intent.ACTION_SENDTO); 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
ACTION_SENDTO
action and include the"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
Telephony
.Web Browser
Load a web URL
ActionTo open a web page, use theACTION_VIEW
action and specify the web URL in the intent data.-
ACTION_VIEW
Data URI Scheme
-
http:<URL>
https:<URL>
MIME Type
-
"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.
Verify Intents with the Android Debug Bridge
To verify that your app responds to the intents that you want to support, you can use the
adb
tool to fire specific intents:- Set up an Android device for development, or use a virtual device.
- Install a version of your app that handles the intents you want to support.
- Fire an intent using
adb
:adb shell am start -a <ACTION> -t <MIME_TYPE> -d <DATA> \ -e <EXTRA_NAME> <EXTRA_VALUE> -n <ACTIVITY>
For example:
adb shell am start -a android.intent.action.DIAL \ -d tel:555-5555 -n org.example.MyApp/.MyActivity
- If you defined the required intent filters, your app should handle the intent.
For more information, see ADB Shell Commands.
Intents Fired by Google Now
Google Now recognizes many voice commands and fires intents for them. As such, users may launch your app with a Google Now voice command if your app declares the corresponding intent filter. For example, if your app can set an alarm and you add the corresponding intent filter to your manifest file, Google Now lets users choose your app when they request to set an alarm, as shown in figure 1.
Google Now recognizes voice commands for the actions listed in table 1. For more information about declaring each intent filter, click on the action description.
一个意图可以让你通过描述一个简单的动作,你想执行(如“查看地图”或“拍照”)在启动其他应用程式的活动
意图
对象。这种类型的意图被称为隐含的意图,因为它没有指定的应用程序组件开始,而是指定了一个动作,并提供了一些 数据,用以执行的操作。当你调用
startActivity()
或startActivityForResult()
并将它传递一个隐含的意图,该系统解决了意向到一个应用程序,可以处理这个意图并开始相应的活动
。如果有一个以上的应用程序,可以处理这个意图,系统显示一个对话框,用户挑选使用哪个应用程序。此页面介绍了可用于执行共同行动,通过处理该意图的应用类型组织了多次隐含意图。每个部分还展示了如何创建一个意图过滤器来宣传您的应用程序的执行相同的操作能力。
注意:如果有可以接收隐含意图在设备上安装任何应用,当它调用您的应用程序会崩溃
startActivity()
。首先验证一个应用程序是否存在接收的意图,调用resolveActivity()
您的意向
对象。如果结果是非空,至少有一个应用程序能够处理的意图和它的安全调用startActivity()
。如果结果为空,你不应该使用的意图,可能的话,你应该禁用调用的意图的功能。如果你不熟悉如何创建意图或意图过滤器,您应该先阅读 意图和意图过滤器。
要了解如何激发您的开发主机此页面上列出的意图,看到 验证了Android调试桥意图。
现在谷歌
谷歌现在触发一些响应语音命令在此页面上列出的意图的。欲了解更多信息,请参阅 由谷歌解雇现在意图。
闹钟
创建一个报警
注:只有小时,分钟和额外的消息在的Android 2.3(API等级9)和更高可用。在该平台的后续版本中添加了其他额外。要创建一个新的警报,使用ACTION_SET_ALARM
行动,并指定报警的详细信息,如下面定义的时间和使用消息临时演员。-
行动
-
ACTION_SET_ALARM
数据URI
- 没有 MIME类型
- 没有 附加功能
-
- 报警的时间。
- 该分钟报警。
- 自定义消息,识别报警。
-
一个
ArrayList中
包括每个工作日上应重复此警报。每天必须从一个整数声明日历
类,如周一
。对于一次性报警,不指定该额外费用。
-
一个
内容:
URI指定一个铃声与报警,或者使用VALUE_RINGTONE_SILENT
无铃声。要使用默认的铃声,不指定该额外费用。
- 一个布尔值指定是否为此报警振动。
- 一个布尔值,指定设置报警时响应的应用程序是否应该跳过它的UI。如果为true,应用程序应该绕过任何确认的用户界面和简单的设置指定的报警。
EXTRA_HOUR
EXTRA_MINUTES
EXTRA_MESSAGE
EXTRA_DAYS
EXTRA_RINGTONE
EXTRA_VIBRATE
EXTRA_SKIP_UI
举例意图:
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 ); } }
注意:为了调用
ACTION_SET_ALARM
意图,您的应用程序必须有SET_ALARM
权限:<使用许可权 的android:名称= “com.android.alarm.permission.SET_ALARM” />
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.SET_ALARM" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
创建一个定时器
注:此意图是在Android 4.4系统(API级别19)补充道。要创建一个倒数计时器,使用ACTION_SET_TIMER
行动,并指定计时器的细节,如使用下面定义的额外时间。-
行动
-
ACTION_SET_TIMER
数据URI
- 没有 MIME类型
- 没有 附加功能
-
- 计时器的长度(秒)。
- 自定义消息,以确定定时器。
- 一个布尔值,指定设置定时器,当响应应用程序是否应该跳过它的UI。如果为true,应用程序应该绕过任何确认的用户界面和简单的启动指定的定时器。
EXTRA_LENGTH
EXTRA_MESSAGE
EXTRA_SKIP_UI
举例意图:
public void startTimer ( String message , int seconds ) { Intent intent = new Intent ( AlarmClock . ACTION_SET_TIMER ) . putExtra ( AlarmClock . EXTRA_MESSAGE , message ) . putExtra ( AlarmClock . EXTRA_LENGTH , seconds ) . putExtra ( AlarmClock . EXTRA_SKIP_UI , true ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
注意:为了调用
ACTION_SET_TIMER
意图,您的应用程序必须有SET_ALARM
权限:<使用许可权 的android:名称= “com.android.alarm.permission.SET_ALARM” />
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.SET_TIMER" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
显示所有报警
要显示报警的列表,使用
ACTION_SHOW_ALARMS
行动。虽然没有太多的应用程序将调用这个目的(它主要由系统的应用程序),它表现为一个闹钟任何应用程序应实现此意图过滤器,并显示当前报警的列表回应。
注:此意图是在Android 4.4系统(API级别19)补充道。
-
行动
-
ACTION_SHOW_ALARMS
数据URI
- 没有 MIME类型
- 没有
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.SHOW_ALARMS" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
日历
添加日历事件
若要将新事件添加到用户的日历,使用
ACTION_INSERT
行动,并指定与数据URIEvents.CONTENT_URI
。然后,您可以使用下列定义各种额外活动详情。-
行动
-
ACTION_INSERT
数据URI
-
Events.CONTENT_URI
MIME类型
-
“vnd.android.cursor.dir /事件”
附加功能
-
- 一个布尔值,指定是否这是一个全天事件。
- 事件的开始时间(纪元以来的毫秒)。
- 事件的结束时间(纪元以来的毫秒)。
- 该事件的标题。
- 事件描述。
- 该事件的位置。
- 以逗号分隔的电子邮件地址列表指定的受邀者。
EXTRA_EVENT_ALL_DAY
EXTRA_EVENT_BEGIN_TIME
EXTRA_EVENT_END_TIME
标题
描述
EVENT_LOCATION
EXTRA_EMAIL
更多活动详情可通过在中定义的常量指定
CalendarContract.EventsColumns
类。
举例意图:
public void addEvent ( String title , String location , Calendar begin , Calendar end ) { Intent intent = new Intent ( Intent . ACTION_INSERT ) . setData ( Events . CONTENT_URI ) . putExtra ( Events . TITLE , title ) . putExtra ( Events . EVENT_LOCATION , location ) . putExtra ( CalendarContract . EXTRA_EVENT_BEGIN_TIME , begin ) . putExtra ( CalendarContract . EXTRA_EVENT_END_TIME , end ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.INSERT" /> <data android:mimeType = "vnd.android.cursor.dir/event" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
相机
拍摄图片或视频,并将其返回
要打开相机应用和接收所产生的照片或视频,使用
ACTION_IMAGE_CAPTURE
或ACTION_VIDEO_CAPTURE
行动。同时指定的URI位置您希望相机保存照片或视频,在EXTRA_OUTPUT
额外费用。-
行动
-
ACTION_IMAGE_CAPTURE
或ACTION_VIDEO_CAPTURE
数据URI方案
- 没有 MIME类型
- 没有 附加功能
-
-
该URI位置,摄像头应用程序应保存照片或视频文件(作为一个
开放的
对象)。
EXTRA_OUTPUT
-
该URI位置,摄像头应用程序应保存照片或视频文件(作为一个
当相机应用成功返回集中到你的活动(您的应用程序接收到
的onActivityResult()
回调),你可以访问你用指定的URI中的照片或视频EXTRA_OUTPUT
值。注意:当您使用
ACTION_IMAGE_CAPTURE
来拍摄照片,相机还可以返回结果中的照片的复印件缩小(缩略图)意图
,保存为一个位图
中提及的额外字段“数据”
。举例意图:
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" ); //做其他工作,保存在mLocationForPhotos全尺寸的照片 ... } }
有关如何使用此意图拍摄照片,包括如何建立一个适当的详细信息,
乌里
为输出位置,阅读 拍照简单或 拍摄视频简单。举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.media.action.IMAGE_CAPTURE" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
在处理这个意图,您的活动应该检查
EXTRA_OUTPUT
传入额外的意图
,然后保存所拍摄的图像或视频在通过额外指定的位置,并调用的setResult()
与意向
,其中包括在名为额外的压缩缩略图“数据“
。在静止图像模式下启动相机应用
行动要打开静态图像模式的相机应用程序,使用INTENT_ACTION_STILL_IMAGE_CAMERA
行动。-
INTENT_ACTION_STILL_IMAGE_CAMERA
数据URI方案
- 没有 MIME类型
- 没有 附加功能
- 没有
举例意图:
public void capturePhoto () { Intent intent = new Intent ( MediaStore . INTENT_ACTION_STILL_IMAGE_CAMERA ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivityForResult ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.media.action.STILL_IMAGE_CAMERA" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
开始在视频模式下相机应用
行动要打开视频模式的相机应用程序,使用INTENT_ACTION_VIDEO_CAMERA
行动。-
INTENT_ACTION_VIDEO_CAMERA
数据URI方案
- 没有 MIME类型
- 没有 附加功能
- 没有
举例意图:
public void capturePhoto () { Intent intent = new Intent ( MediaStore . INTENT_ACTION_VIDEO_CAMERA ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivityForResult ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.media.action.VIDEO_CAMERA" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
联系人/联系人应用
选择一个联系人
让用户选择一个联系人,并提供给所有的联系人信息,您的应用程序访问,请使用
ACTION_PICK
行动,并指定MIME类型Contacts.CONTENT_TYPE
。结果
意向
发送到您的onActivityResult()
回调包含内容:
URI指向选定的联系人。响应授予您的应用程序的临时权限读取用的接触联系提供 API,即使你的应用程序不包含READ_CONTACTS
许可。提示:如果您需要访问只是一段特定的联系信息,如电话号码或电子邮件地址,而不是看到有关如何下一节 选择特定的联系人数据。
-
行动
-
ACTION_PICK
数据URI方案
- 没有 MIME类型
-
Contacts.CONTENT_TYPE
举例意图:
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 (); //做一些与contactUri选定联系人 ... } }
有关如何获取联系方式,一旦你取得联系URI信息,请阅读获取详细的联系方式。记住,当你检索联系人URI与上面的意图,你 并不需要
READ_CONTACTS
权限读取该联系人的详细信息。选择特定的联系人数据
为了让用户选择一个联系人,信息的特定部分,如电话号码,电子邮件地址或其他数据类型,使用
ACTION_PICK
行动,并指定MIME类型下面列出的内容类型,如之一CommonDataKinds。 Phone.CONTENT_TYPE
获取联系人的电话号码。如果需要从一个接触检索只有一种类型的数据,该技术具有
CONTENT_TYPE
从ContactsContract.CommonDataKinds
类是比使用更有效Contacts.CONTENT_TYPE
(如前一节中示出),因为结果为您提供直接访问使用时无需所需的数据以执行一个更复杂的查询联系人提供商。结果
意向
发送到您的onActivityResult()
回调包含内容:
URI指向选定的联系人数据。响应授予您的应用程序的临时权限读取联系人数据,即使您的应用程序不包含READ_CONTACTS
许可。-
行动
-
ACTION_PICK
数据URI方案
- 没有 MIME类型
-
- 从电话号码的联系人挑选。
- 从电子邮件地址的联系人挑选。
- 从邮政地址联系方式挑选。
CommonDataKinds.Phone.CONTENT_TYPE
CommonDataKinds.Email.CONTENT_TYPE
CommonDataKinds.StructuredPostal.CONTENT_TYPE
许多其他的或者一个
CONTENT_TYPE
下值ContactsContract
。
举例意图:
静态 最终 INT REQUEST_SELECT_PHONE_NUMBER = 1 ; 公共 无效selectContact () { //开始一个活动的用户选择从一个电话号码 得到URI和查询内容提供商的电话 如果返回的指针是有效的,拿到手机 做一些与电话号码 ... } } }
查看联系人
要为一个已知的联系人显示详细信息,可以使用
ACTION_VIEW
行动,并指定了联系人内容:
URI的意图数据。有主要有两种方式来最初检索联系人的URI:
- 使用由返回的接触URI
ACTION_PICK
,在上一节中显示(这种方法不需要任何应用程序的权限)。 - 直接访问所有联系人的列表,如在描述检索联系人列表(此方法需要
READ_CONTACTS
权限)。
-
行动
-
ACTION_VIEW
数据URI方案
-
内容:<URI>
MIME类型
- 没有。类型是从接触的URI推断。
举例意图:
public void viewContact ( Uri contactUri ) { Intent intent = new Intent ( Intent . ACTION_VIEW , contactUri ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
编辑现有的联系人
要编辑一个已知的接触,使用
ACTION_EDIT
动作,请用联系的内容:
URI的意图数据,并且包括在常量指定额外任何已知的联系人信息ContactsContract.Intents.Insert
。有主要有两种方式来最初检索联系人的URI:
- 使用由返回的接触URI
ACTION_PICK
,在上一节中显示(这种方法不需要任何应用程序的权限)。 - 直接访问所有联系人的列表,如在描述检索联系人列表(此方法需要
READ_CONTACTS
权限)。
-
行动
-
ACTION_EDIT
数据URI方案
-
内容:<URI>
MIME类型
- 类型是从接触的URI推断。 附加功能
-
一个或多个定义的群众演员
ContactsContract.Intents.Insert
这样你就可以填充的联系信息的字段。
举例意图:
public void editContact ( Uri contactUri , String email ) { Intent intent = new Intent ( Intent . ACTION_EDIT ); intent . setData ( contactUri ); intent . putExtra ( Intents . Insert . EMAIL , email ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
有关如何编辑联系人的详细信息,请参阅修改联系人使用意图。
插入联系人
要插入一个新的联系人,使用
ACTION_INSERT
行动,指定Contacts.CONTENT_TYPE
作为MIME类型,并包括在指定的常量额外任何已知的联系人信息ContactsContract.Intents.Insert
。-
行动
-
ACTION_INSERT
数据URI方案
- 没有 MIME类型
-
Contacts.CONTENT_TYPE
附加功能
-
一个或多个定义的群众演员
ContactsContract.Intents.Insert
。
举例意图:
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 ); } }
有关如何插入联系人的详细信息,请参阅修改联系人使用意图。
电子邮件
撰写具有可选附件的电子邮件
要编写电子邮件,使用基于您是否会包含附件下列行为之一的,和包括电子邮件的详细信息,如收件人并受使用下面列出的额外的钥匙。
-
行动
-
ACTION_SENDTO
(无附件)或ACTION_SEND
(一个附件)或ACTION_SEND_MULTIPLE
(多个附件)
数据URI方案
- 没有 MIME类型
-
“text / plain的”
“* / *”
附加功能
-
- 一个字符串数组所有“到”收件人的电子邮件地址。
- 一个字符串数组,所有的“CC”的收件人的电子邮件地址。
- 一个字符串数组,所有的“密件抄送”收件人的电子邮件地址。
- 字符串的邮件主题。
- 一个字符串与电子邮件的正文。
-
一个
开放的
指向附件。如果使用的是ACTION_SEND_MULTIPLE
动作,这应该是不是一个ArrayList中
包含多个开放的
对象。
Intent.EXTRA_EMAIL
Intent.EXTRA_CC
Intent.EXTRA_BCC
Intent.EXTRA_SUBJECT
Intent.EXTRA_TEXT
Intent.EXTRA_STREAM
举例意图:
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 ); } }
如果你想确保你的意图是通过电子邮件应用程序(而不是其他短信或社交应用)才处理,然后用
ACTION_SENDTO
行动,包括“电子邮件地址:”
数据方案。例如:public void composeEmail ( String [] addresses , String subject ) { Intent intent = new Intent ( Intent . ACTION_SENDTO ); intent . setData ( Uri . parse ( "mailto:" )); //只有电子邮件应用应如何处理this intent . putExtra ( Intent . EXTRA_EMAIL , addresses ); intent . putExtra ( Intent . EXTRA_SUBJECT , subject ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<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>
文件存储
检索特定类型的文件
要请求用户选择一个文件,如文档或照片,并返回一个引用您的应用程序,使用
ACTION_GET_CONTENT
行动,并指定所需的MIME类型。引用返回到您的应用程序文件是短暂的,以您的活动目前的生命周期,因此,如果您要访问它后你必须导入一个副本,你可以在以后阅读。此意图也允许用户创建的过程中一个新的文件(例如,而不是选择现有照片时,用户可以捕捉与相机新照片)。送到你的结果意图
的onActivityResult()
方法包括用一个URI指向文件数据。该URI可以是任何东西,如HTTP:
URI,文件:
URI或内容:
URI。不过,如果你想选择的文件限制为只有那些从访问内容提供商(一个内容:
URI),并且可作为一个文件流openFileDescriptor()
,你应该添加CATEGORY_OPENABLE
类别你的意图。在Android 4.3(API级别18)和更高,还可以让用户通过添加选择多个文件
EXTRA_ALLOW_MULTIPLE
的意图,设置为真
。然后,您可以访问每个选定的文件在ClipData
通过返回的对象()getClipData
。-
行动
-
ACTION_GET_CONTENT
数据URI方案
- 没有 MIME类型
- 相应的文件类型,用户应选择的MIME类型。 附加功能
-
- 一个布尔宣布用户是否可以一次选择多个文件。
- 声明返回的文件是否必须是可直接从设备,而不需要从远程服务下载的布尔值。
EXTRA_ALLOW_MULTIPLE
EXTRA_LOCAL_ONLY
类别(可选)
-
-
为返回可被表示为文件流只有“开”的文件
openFileDescriptor()
。
CATEGORY_OPENABLE
-
为返回可被表示为文件流只有“开”的文件
例如意图得到一张照片:
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 (); //做保存在fullPhotoUri照片的工作 ...... } }
例如意图过滤器返回的照片:
<activity ... > <intent-filter> <action android:name = "android.intent.action.GET_CONTENT" /> <data android:type = "image/*" /> <category android:name = "android.intent.category.DEFAULT" /> <!--可打开类声明,返回的文件是可访问 来自内容提供商supports
OpenableColumns
andContentResolver.openFileDescriptor()
--> <category android:name = "android.intent.category.OPENABLE" /> </intent-filter> </activity>打开特定类型的文件
相反,检索,你必须导入到你的应用程序文件的副本(通过使用
ACTION_GET_CONTENT
动作),在Android 4.4或更高版本上运行时,可以改为请求打开使用的是由另一个应用程序管理的文件ACTION_OPEN_DOCUMENT
行动,指定MIME类型。为了还允许用户,而不是创建一个新的文件,你的应用程序可以写入,使用ACTION_CREATE_DOCUMENT
动作来代替。例如,而不是从现有的PDF文档中选择时,ACTION_CREATE_DOCUMENT
意图允许用户选择他们想要创建一个新文件(管理文件的存储另一个应用程序内) -您的应用然后接收的地方可以在URI位置写入新文件。而意图传递给你
的onActivityResult()
方法从ACTION_GET_CONTENT
动作可以返回任何类型的URI,从结果意图ACTION_OPEN_DOCUMENT
和ACTION_CREATE_DOCUMENT
始终指定所选择的文件作为内容:
URI这是由一个支持DocumentsProvider
。您可以打开与文件openFileDescriptor()
,并使用从列查询其详细信息DocumentsContract.Document
。返回URI赋予您的应用程序的长期读取访问文件(也可能与写访问)。所以
ACTION_OPEN_DOCUMENT
动作是非常有用的(而不是使用ACTION_GET_CONTENT
当你想读一个现有的文件不进行复制到你的应用程序,或当你想打开和就地编辑的文件)。您也可以允许用户通过添加选择多个文件
EXTRA_ALLOW_MULTIPLE
的意图,设置为真
。如果用户选择只有一个项目,然后你可以从该项目的getData()
。如果用户选择一个以上的项目,然后的getData()
返回null,则必须改为从检索每个项目ClipData
由返回的对象getClipData()
。注意:您的意图必须指定一个MIME类型和 必须申报
CATEGORY_OPENABLE
类别。如果合适的话,您可以通过添加MIME类型的数组与指定多个MIME类型EXTRA_MIME_TYPES
外,如果你这样做,你必须设置在主MIME类型的setType()
,以“* / *”
。-
行动
-
ACTION_OPEN_DOCUMENT
或ACTION_CREATE_DOCUMENT
数据URI方案
- 没有 MIME类型
- 相应的文件类型,用户应选择的MIME类型。 附加功能
-
-
对应类型的应用请求文件的MIME类型的数组。当您使用此额外的,你必须设置在主MIME类型
的setType()
,以“* / *”
。 - 声明用户是否可以一次选择多个文件的布尔值。
-
对于使用
ACTION_CREATE_DOCUMENT
到指定初始文件名 。 - 声明返回的文件是否必须是可直接从设备,而不需要从远程服务下载的布尔值。
EXTRA_MIME_TYPES
EXTRA_ALLOW_MULTIPLE
EXTRA_TITLE
EXTRA_LOCAL_ONLY
类别
-
对应类型的应用请求文件的MIME类型的数组。当您使用此额外的,你必须设置在主MIME类型
-
-
为返回可被表示为文件流只有“开”的文件
openFileDescriptor()
。
CATEGORY_OPENABLE
-
为返回可被表示为文件流只有“开”的文件
例如意图得到一张照片:
只有系统接收ACTION_OPEN_DOCUMENT,所以没必要 做保存在fullPhotoUri全尺寸照片的工作 ... } }
第三方应用程序不能真正与意图做出回应
ACTION_OPEN_DOCUMENT
行动。相反,系统接收到这个意图,并显示在一个统一的用户界面可从各种应用程序的所有文件。为了提供您的应用程序的文件中的用户界面,并允许其他应用程序打开它们,你必须实现
DocumentsProvider
并包括一个意图过滤器PROVIDER_INTERFACE
(“android.content.action.DOCUMENTS_PROVIDER”
)。例如:<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>
有关如何使您的应用程序从其他应用程序打开的管理文件的更多信息,请阅读存储访问架构指南。
本地操作
叫车
注:在完成动作之前必须应用要求确认从用户。要叫出租车,使用ACTION_RESERVE_TAXI_RESERVATION
行动。-
行动
-
ACTION_RESERVE_TAXI_RESERVATION
数据URI
- 没有 MIME类型
- 没有 附加功能
- 没有
举例意图:
public void callCar () { Intent intent = new Intent ( ReserveIntents . ACTION_RESERVE_TAXI_RESERVATION ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "com.google.android.gms.actions.RESERVE_TAXI_RESERVATION" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
地图
在地图上显示的位置
打开一个地图,使用
ACTION_VIEW
动作,并与下面定义的方案之一中指定的意图数据的位置信息。-
行动
-
ACTION_VIEW
数据URI方案
-
-
显示在给定的经度和纬度的地图。
例如:
“地球:47.6,-122.3”
-
显示在给定的经度的地图,并在一定的缩放级别纬度。1缩放级别显示了整个地球,在给定的中心
纬度,
经度。最高(最近)的缩放级别是23。
例如:
“地球:47.6,-122.3 Z = 11”
-
显示在给定的经度地图,并用字符串标签纬度。
例如:
“地球:0,0 Q = 34.99,-106.61(宝物)”
-
显示“我的街道地址”的位置(可以是一个特定的地址或位置查询)。
例如:
“地球:0,0 Q = 1600 +剧场+百汇%2C + CA”
注:在通过所有字符串
地理
URI必须进行编码。例如,该串第一和派克,西雅图
应成为第1%20%26%20Pike%2C%20Seattle
。该字符串中的空格可以与编码%20
或加号(更换+
)。
地理:纬度,经度
地理:纬度,经度?Z = 缩放
地理:0,0 Q =纬度,经度(标签)
地理:0,0 Q =我+街道+地址
MIME类型
-
显示在给定的经度和纬度的地图。
- 没有
举例意图:
public void showMap ( Uri geoLocation ) { Intent intent = new Intent ( Intent . ACTION_VIEW ); intent . setData ( geoLocation ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.VIEW" /> <data android:scheme = "geo" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
音乐或视频
播放媒体文件
播放音乐文件,使用
ACTION_VIEW
行动和意图的数据指定文件的URI位置。-
行动
-
ACTION_VIEW
数据URI方案
-
文件:<URI>
内容:<URI>
HTTP:<网址>
MIME类型
-
“音频/ *”
“应用程序/ OGG”
“应用程序/ x-OGG”
“应用程序/ iTunes的”
或其他任何您的应用程序可能需要。
举例意图:
public void playMedia ( Uri file ) { Intent intent = new Intent ( Intent . ACTION_VIEW ); intent . setData ( file ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<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>
基于搜索查询播放音乐
这个意图应该包括EXTRA_MEDIA_FOCUS
串额外的,它指定了inteded搜索模式。例如,搜索模式可以指定搜索内容是一个艺术家姓名或歌曲名称。要播放基于搜索查询的音乐,使用INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
意图。一个应用程序可能会触发此意图响应用户的语音命令来播放音乐。这个意图接收应用程序执行其清单中搜索匹配现有的内容,以给定的查询,并开始播放这些内容。-
行动
-
INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
数据URI方案
- 没有 MIME类型
- 没有 附加功能
-
-
指示搜索模式(用户是否正在寻找一个特定的艺术家,专辑,歌曲或播放列表)。大多数搜索模式,采取额外的附加 。例如,如果用户有兴趣听特定的歌曲,其目的可能有三个额外的附加 :歌曲名称,艺术家和专辑。此意向支持的每个值以下搜索模式
EXTRA_MEDIA_FOCUS
:-
播放任何音乐。接收应用程序应发挥基于一个聪明的选择,一些音乐如最后播放列表用户听。
额外的附加:
QUERY
(必需) -空字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
-
从非结构化搜索查询播放特定的歌曲,专辑或流派。应用程序可能会产生这种搜索模式时,他们无法识别用户想要收听的内容类型的意图。可能的情况下应用程序应该使用更具体的搜索模式。
额外的附加:
QUERY
(必填) -包含的任意组合的字符串:艺术家,专辑,歌曲名称或流派。
-
播放特定类型的音乐。
额外的附加:
“android.intent.extra.genre”
(必需) -流派。QUERY
(必需) -流派。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
-
从特定艺术家播放音乐。
额外的附加:
EXTRA_MEDIA_ARTIST
(必需) -艺术家。“android.intent.extra.genre”
-流派。QUERY
(必填) -包含艺术家或流派的任意组合的字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
-
从一个特定的专辑播放音乐。
额外的附加:
EXTRA_MEDIA_ALBUM
(必需) -专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。QUERY
(必填) -包含专辑或艺术家的任意组合的字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
-
播放特定的歌曲。
额外的附加:
EXTRA_MEDIA_ALBUM
-专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。EXTRA_MEDIA_TITLE
(必需) -歌曲名。QUERY
(必填) -包含的任意组合的字符串:专辑,艺术家,流派或标题。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
-
播放特定的播放列表,或通过匹配额外附加的一些规定条件的播放列表。
额外的附加:
EXTRA_MEDIA_ALBUM
-专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。“android.intent.extra.playlist”
-播放清单。EXTRA_MEDIA_TITLE
-该播放列表是根据歌曲名称。QUERY
(必填) -包含的任意组合的字符串:专辑,艺术家,流派,播放列表,或称号。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。
任何 -
“vnd.android.cursor.item / *”
非结构化 -
“vnd.android.cursor.item / *”
流派 -
Audio.Genres.ENTRY_CONTENT_TYPE
艺术家 -
Audio.Artists.ENTRY_CONTENT_TYPE
专辑 -
Audio.Albums.ENTRY_CONTENT_TYPE
宋 -
“vnd.android.cursor.item /音频”
播放列表 -
Audio.Playlists.ENTRY_CONTENT_TYPE
-
MediaStore.EXTRA_MEDIA_FOCUS
(必填) -
举例意图:
如果用户想从一个特定的艺术家听音乐,搜索应用程序可能会生成以下意图:
public void playSearchArtist ( String artist ) { Intent intent = new Intent ( MediaStore . INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH ); intent . putExtra ( MediaStore . EXTRA_MEDIA_FOCUS , MediaStore . Audio . Artists . ENTRY_CONTENT_TYPE ); intent . putExtra ( MediaStore . EXTRA_MEDIA_ARTIST , artist ); intent . putExtra ( SearchManager . QUERY , artist ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.media.action.MEDIA_PLAY_FROM_SEARCH" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
在处理这个意图,您的活动应该检查的价值
EXTRA_MEDIA_FOCUS
额外传入意图
来确定搜索模式。一旦你的活动已经确定了搜索模式,它应该阅读额外的附加 的值特定的搜索模式。有了这个信息,你的应用程序就可以执行其库存内的搜索播放相匹配的搜索查询的内容。例如:一些群众演员取决于搜索可能无法使用 确定搜索模式,并使用相应的临时演员 ,如果 (mediaFocus == 空) { //'非结构化'搜索模式(向后 “任何'搜索模式 playResumeLastPlaylist (); } 其他 { //'非结构化'的搜索 '类型'的搜索 “艺术家”搜索 '相册'搜索模式 playAlbum (专辑,艺术家); } 否则 如果 (mediaFocus 。的compareTo (“vnd.android.cursor.item /音频” ) == 0 ) { //'歌'搜索 “播放列表”搜索模式 playPlaylist (专辑,艺术家,流派,播放列表,标题); } } }
新注
创建笔记
要创建一个新的笔记,使用
ACTION_CREATE_NOTE
行动,并指定笔记的详细信息,如下面定义的主题和使用文本演员。注:在完成动作之前必须应用要求确认从用户。
-
行动
-
ACTION_CREATE_NOTE
数据URI方案
- 没有 MIME类型
-
PLAIN_TEXT_TYPE
- “* / *” 附加功能
-
- 一个字符串,指示标题或说明的主题。
- 一个字符串,指示的说明文字。
EXTRA_NAME
EXTRA_TEXT
举例意图:
public void createNote ( String subject , String text ) { Intent intent = new Intent ( NoteIntents . ACTION_CREATE_NOTE ) . putExtra ( NoteIntents . EXTRA_NAME , subject ) . putExtra ( NoteIntents . EXTRA_TEXT , text ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "com.google.android.gms.actions.CREATE_NOTE" /> <category android:name = "android.intent.category.DEFAULT" /> <data android:mimeType = ”*/*” > </intent-filter> </activity>
电话
发起呼叫
打开手机应用程序,并拨打电话号码,使用
ACTION_DIAL
行动,并指定使用下面定义的URI方案的电话号码。当手机应用打开时,它显示的电话号码,但用户必须按下呼叫按钮开始通话。
该ACTION_CALL
操作需要您在添加CALL_PHONE
权限您的清单文件:要直接拨打电话,使用ACTION_CALL
行动,并指定使用下面定义的URI方案的电话号码。当手机应用打开时,它开始了电话; 用户并不需要按呼叫按钮。<使用许可权 的android:名称= “android.permission.CALL_PHONE” />
-
行动
-
ACTION_DIAL
-打开拨号器或手机应用程序。ACTION_CALL
-地点电话(需要CALL_PHONE
许可)
数据URI方案
-
联系电话:<电话号码>
语音信箱:<电话号码>
MIME类型
- 没有
有效的电话号码是那些在所定义的IETF RFC 3966有效实例包括以下:
联系电话:2125551212
电话:(212)555 1212
手机的拨号善于正常化方案,如电话号码。所以描述的方案是不严格要求在
Uri.parse()
方法。但是,如果你还没有尝试过的计划或不确定是否可以处理,使用Uri.fromParts()
方法来代替。举例意图:
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 ); } }
搜索
搜索使用某特定应用
在您的应用程序进行语音搜索为了支持你的应用程序的范围内搜索,与您的应用程序声明意图过滤器
SEARCH_ACTION
动作,如下面意图过滤的例子。-
行动
-
- 支持谷歌搜索查询现在。
“com.google.android.gms.actions.SEARCH_ACTION”
附加功能
-
-
包含搜索查询的字符串。
QUERY
-
包含搜索查询的字符串。
举例意图过滤器:
<activity android:name = ".SearchActivity" > <intent-filter> <action android:name = "com.google.android.gms.actions.SEARCH_ACTION" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
执行网络搜索
要启动网络搜索,使用
ACTION_WEB_SEARCH
行动,并指定要在搜索字符串SearchManager.QUERY
额外费用。-
行动
-
ACTION_WEB_SEARCH
数据URI方案
- 没有 MIME类型
- 没有 附加功能
-
- 搜索字符串。
SearchManager.QUERY
举例意图:
public void searchWeb ( String query ) { Intent intent = new Intent ( Intent . ACTION_SEARCH ); intent . putExtra ( SearchManager . QUERY , query ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
设置
打开设置的特定部分
要在您的应用程序要求用户更改某些设置,使用下面的意图行动,以打开设置界面相应的操作名称之一打开系统设置的屏幕。
-
行动
-
ACTION_SETTINGS
ACTION_WIRELESS_SETTINGS
ACTION_AIRPLANE_MODE_SETTINGS
ACTION_WIFI_SETTINGS
ACTION_APN_SETTINGS
ACTION_BLUETOOTH_SETTINGS
ACTION_DATE_SETTINGS
ACTION_LOCALE_SETTINGS
ACTION_INPUT_METHOD_SETTINGS
ACTION_DISPLAY_SETTINGS
ACTION_SECURITY_SETTINGS
ACTION_LOCATION_SOURCE_SETTINGS
ACTION_INTERNAL_STORAGE_SETTINGS
ACTION_MEMORY_CARD_SETTINGS
查看
设置
为其他设置屏幕可用文档。
数据URI方案
- 没有 MIME类型
- 没有
举例意图:
public void openWifiSettings () { Intent intent = new Intent ( Intent . ACTION_WIFI_SETTINGS ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
短信
缀以附件的短信/彩信
要启动SMS或MMS短信,使用下面的意图行动之一,并指定消息的详细信息,如电话号码,主题和邮件正文使用下面列出的额外的钥匙。
-
行动
-
ACTION_SENDTO
或ACTION_SEND
或ACTION_SEND_MULTIPLE
数据URI方案
-
短信:<PHONE_NUMBER>
smsto:<PHONE_NUMBER>
彩信:<PHONE_NUMBER>
mmsto:<PHONE_NUMBER>
所有这些方案的处理方式相同。
MIME类型
-
“text / plain的”
“图片/*”
“视频/*”
附加功能
-
- 一种邮件主题的字符串(通常仅MMS)。
- 一个字符串文本消息。
-
一个
开放的
指向图像或视频附加。如果使用的是ACTION_SEND_MULTIPLE
动作,这个额外的应该是一个ArrayList中
的开放的
指点下到图像/视频附加。
“学科”
“SMS_BODY”
EXTRA_STREAM
举例意图:
public void composeMmsMessage ( String message , Uri attachment ) { Intent intent = new Intent ( Intent . ACTION_SENDTO ); intent . setType ( HTTP . PLAIN_TEXT_TYPE ); intent . putExtra ( "sms_body" , message ); intent . putExtra ( Intent . EXTRA_STREAM , attachment ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
如果你想确保你的意图是通过短信的应用程序(而不是其他电子邮件或社交应用程序)才处理,然后用
ACTION_SENDTO
行动,包括“smsto:”
数据方案。例如:public void composeMmsMessage ( String message , Uri attachment ) { Intent intent = new Intent ( Intent . ACTION_SEND ); intent . setData ( Uri . parse ( "smsto:" )); //这可确保只有短信应用respond intent . putExtra ( "sms_body" , message ); intent . putExtra ( Intent . EXTRA_STREAM , attachment ); if ( intent . resolveActivity ( getPackageManager ()) != null ) { startActivity ( intent ); } }
举例意图过滤器:
<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>
注:如果您正在开发的SMS / MMS短信应用程序,您必须实现意图过滤器对几个附加的行为,以便可作为 默认的短信应用在Android 4.4及更高版本。欲了解更多信息,请阅读文档
电话
。网页浏览器
加载网页的网址
行动要打开一个网页,使用ACTION_VIEW
行动,并在意向数据指定Web URL。-
ACTION_VIEW
数据URI方案
-
HTTP:<网址>
的https:<网址>
MIME类型
-
“text / plain的”
“text / html的”
“是application / xhtml + XML”
“应用程序/ vnd.wap.xhtml + XML”
举例意图:
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 ); } }
举例意图过滤器:
<活动... > <意图过滤器> <行动 机器人:名字= “android.intent.action.VIEW” /> < -包括主机属性,如果你想你的应用程序响应! 只与您的应用程序的网域网址。 可浏览的类别需要得到从网页链接。- > <类 机器人:名字= “android.intent.category.BROWSABLE” /> </意图过滤> </活动>
提示:如果您的Android应用程序提供了类似于你的网站的功能,包括指向你的网站的网址意图过滤器。然后,如果用户安装了您的应用程序,从电子邮件或其他网页指向你的网站打开你的Android应用程序,而不是你的网页链接。
验证了Android调试桥意图
要验证您的应用程序响应你想要支持的意图,则可以使用
ADB
工具火特定意图:- 设置Android设备的开发,或者使用虚拟设备。
- 安装一个版本的应用程序的处理要支持的意图。
- 消防使用意图
ADB
:亚行的shell时开始-a <ACTION> -t <MIME_TYPE> -d <DATA> \ -e <EXTRA_NAME> <EXTRA_VALUE> -n <活动>
例如:
亚行的shell时开始-a android.intent.action.DIAL \ -d电话:555-5555 -n org.example.MyApp / .MyActivity
- 如果定义所需的意图过滤器,您的应用程序应该处理这个意图。
欲了解更多信息,请参见 ADB Shell命令。
由谷歌解雇现在的意图
谷歌现在认识了很多语音命令和火灾意图他们。因此,用户可以启动应用程序与谷歌一现在,语音命令,如果您的应用程序声明相应的意图过滤器。例如,如果您的应用程序可以 设置闹钟和你对应的意图过滤器添加到您的清单文件,谷歌现在允许用户选择你的应用程序时,他们要求设置闹钟,如图1。
谷歌现在认识到表1中列出有关声明的每个意图过滤器的详细信息的行动声控命令,请单击操作说明。
类别 详细信息和示例 操作名称 报警 - “设置警报7 AM”
AlarmClock.ACTION_SET_ALARM
- “设置一个计时器5分钟”
AlarmClock.ACTION_SET_TIMER
通讯 - “呼555-5555”
- “呼叫鲍勃”
- “电话语音信箱”
Intent.ACTION_CALL
本地 - “叫我车”
- “我订了一辆出租车”
ReserveIntents
.ACTION_RESERVE_TAXI_RESERVATION媒体 - “玩迈克尔·杰克逊比莉珍”
MediaStore
.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH- “拍照”
MediaStore
.INTENT_ACTION_STILL_IMAGE_CAMERA- “录制视频”
MediaStore
.INTENT_ACTION_VIDEO_CAMERA搜索 - “寻找猫的视频
在myvideoapp”
“com.google.android.gms.actions
.SEARCH_ACTION”网页浏览器 - “开放example.com”
Intent.ACTION_VIEW