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 fires some of the intents listed on this page in response to voice commands. For more information, see Intents Fired by Google Now.
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_SET_ALARM
EXTRA_HOUR
EXTRA_MINUTES
EXTRA_MESSAGE
EXTRA_DAYS
ArrayList
including each week day on which this alarm should be repeated. Each day must be declared with an integer from the
Calendar
class such as
MONDAY
.
For a one-time alarm, do not specify this extra.
EXTRA_RINGTONE
content:
URI specifying a ringtone to use with the alarm, or
VALUE_RINGTONE_SILENT
for no ringtone.
To use the default ringtone, do not specify this extra.
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>
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_SET_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>
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_SHOW_ALARMS
Example intent filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SHOW_ALARMS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
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_INSERT
Events.CONTENT_URI
"vnd.android.cursor.dir/event"
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 theCalendarContract.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>
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_IMAGE_CAPTURE
or
ACTION_VIDEO_CAPTURE
EXTRA_OUTPUT
Uri
object).
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"
.
ActionTo open a camera app in still image mode, use theINTENT_ACTION_STILL_IMAGE_CAMERA
action.
INTENT_ACTION_STILL_IMAGE_CAMERA
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>
ActionTo open a camera app in video mode, use theINTENT_ACTION_VIDEO_CAMERA
action.
INTENT_ACTION_VIDEO_CAMERA
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>
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_PICK
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.
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_PICK
CommonDataKinds.Phone.CONTENT_TYPE
CommonDataKinds.Email.CONTENT_TYPE
CommonDataKinds.StructuredPostal.CONTENT_TYPE
Or one of many other CONTENT_TYPE
values under 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 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:
ACTION_PICK
, shown in the previous section (this approach does not require any app permissions).READ_CONTACTS
permission).ACTION_VIEW
content:<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 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:
ACTION_PICK
, shown in the previous section (this approach does not require any app permissions).READ_CONTACTS
permission).ACTION_EDIT
content:<URI>
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.
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_INSERT
Contacts.CONTENT_TYPE
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.
ACTION_SENDTO
(for no attachment) or
ACTION_SEND
(for one attachment) or
ACTION_SEND_MULTIPLE
(for multiple attachments)
"text/plain"
"*/*"
Intent.EXTRA_EMAIL
Intent.EXTRA_CC
Intent.EXTRA_BCC
Intent.EXTRA_SUBJECT
Intent.EXTRA_TEXT
Intent.EXTRA_STREAM
Uri
pointing to the attachment. If using the
ACTION_SEND_MULTIPLE
action, this should instead be an
ArrayList
containing multiple
Uri
objects.
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>
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_GET_CONTENT
EXTRA_ALLOW_MULTIPLE
EXTRA_LOCAL_ONLY
CATEGORY_OPENABLE
openFileDescriptor()
.
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>
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_OPEN_DOCUMENT
or
ACTION_CREATE_DOCUMENT
EXTRA_MIME_TYPES
setType()
to
"*/*"
.
EXTRA_ALLOW_MULTIPLE
EXTRA_TITLE
ACTION_CREATE_DOCUMENT
to specify an initial file name.
EXTRA_LOCAL_ONLY
CATEGORY_OPENABLE
openFileDescriptor()
.
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.
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_RESERVE_TAXI_RESERVATION
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>
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_VIEW
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%2C+CA"
Note: All strings passed in the geo
URI must be encoded. For example, the string 1st & Pike, Seattle
should become 1st%20%26%20Pike%2C%20Seattle
. Spaces in the string can be encoded with %20
or replaced with the plus sign (+
).
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 ACTION_VIEW
action and specify the URI location of the file in the intent data.
ACTION_VIEW
file:<URI>
content:<URI>
http:<URL>
"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>
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.
INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
MediaStore.EXTRA_MEDIA_FOCUS
(required)
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
:
Any - "vnd.android.cursor.item/*"
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.Unstructured - "vnd.android.cursor.item/*"
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.Genre - Audio.Genres.ENTRY_CONTENT_TYPE
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.Artist - Audio.Artists.ENTRY_CONTENT_TYPE
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.Album - Audio.Albums.ENTRY_CONTENT_TYPE
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.Song - "vnd.android.cursor.item/audio"
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.Playlist - Audio.Playlists.ENTRY_CONTENT_TYPE
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.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); } } }
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 | Set alarm
|
AlarmClock.ACTION_SET_ALARM |
Set timer
|
AlarmClock.ACTION_SET_TIMER |
|
Communication | Call a number
|
Intent.ACTION_CALL |
Local | Book a car
|
ReserveIntents |
Media | Play music from search
|
MediaStore |
Take a picture
|
MediaStore |
|
Record a video
|
MediaStore |
|
Search | Search using a specific app
|
"com.google.android.gms.actions |
Web browser | Open URL
|
Intent.ACTION_VIEW |
ACTION_CREATE_NOTE
PLAIN_TEXT_TYPE
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>
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.
The ACTION_CALL
action requires that you add the CALL_PHONE
permission to your manifest file:To place a phone call directly, use the ACTION_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_DIAL
- Opens the dialer or phone app.ACTION_CALL
- Places a phone call (requires the CALL_PHONE
permission)tel:<phone-number>
voicemail:<phone-number>
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 the 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); } }
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.
"com.google.android.gms.actions.SEARCH_ACTION"
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>
To initiate a web search, use the ACTION_WEB_SEARCH
action and specify the search string in theSearchManager.QUERY
extra.
ACTION_WEB_SEARCH
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); } }
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_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.
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.
ACTION_SENDTO
or
ACTION_SEND
or
ACTION_SEND_MULTIPLE
sms:<phone_number>
smsto:<phone_number>
mms:<phone_number>
mmsto:<phone_number>
Each of these schemes are handled the same.
"text/plain"
"image/*"
"video/*"
"subject"
"sms_body"
EXTRA_STREAM
Uri
pointing to the image or video to attach. If using the
ACTION_SEND_MULTIPLE
action, this extra should be an
ArrayList
of
Uri
s pointing to the images/videos to attach.
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
.
ActionTo open a web page, use the ACTION_VIEW
action and specify the web URL in the intent data.
ACTION_VIEW
http:<URL>
https:<URL>
"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 verify that your app responds to the intents that you want to support, you can use the adb
tool to fire specific intents:
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
For more information, see ADB Shell Commands.
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.
Figure 1. Google Now lets users choose from installed apps that support a given action.
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.
Table 1. Voice commands recognized by Google Now (Google Search app v3.6)
一个意图可以让你通过描述一个简单的动作,你想执行(如“查看地图”或“拍照”)在启动其他应用程式的活动意图
对象。这种类型的意图被称为隐含的意图,因为它没有指定的应用程序组件开始,而是指定了一个动作,并提供了一些 数据,用以执行的操作。
当你调用 startActivity()
或 startActivityForResult()
并将它传递一个隐含的意图,该系统解决了意向到一个应用程序,可以处理这个意图并开始相应的活动
。如果有一个以上的应用程序,可以处理这个意图,系统显示一个对话框,用户挑选使用哪个应用程序。
此页面介绍了可用于执行共同行动,通过处理该意图的应用类型组织了多次隐含意图。每个部分还展示了如何创建一个意图过滤器来宣传您的应用程序的执行相同的操作能力。
注意:如果有可以接收隐含意图在设备上安装任何应用,当它调用您的应用程序会崩溃startActivity()
。首先验证一个应用程序是否存在接收的意图,调用resolveActivity()
您的意向
对象。如果结果是非空,至少有一个应用程序能够处理的意图和它的安全调用startActivity()
。如果结果为空,你不应该使用的意图,可能的话,你应该禁用调用的意图的功能。
如果你不熟悉如何创建意图或意图过滤器,您应该先阅读 意图和意图过滤器。
要了解如何激发您的开发主机此页面上列出的意图,看到 验证了Android调试桥意图。
谷歌现在触发一些响应语音命令在此页面上列出的意图的。欲了解更多信息,请参阅 由谷歌解雇现在意图。
注:只有小时,分钟和额外的消息在的Android 2.3(API等级9)和更高可用。在该平台的后续版本中添加了其他额外。要创建一个新的警报,使用ACTION_SET_ALARM
行动,并指定报警的详细信息,如下面定义的时间和使用消息临时演员。
ACTION_SET_ALARM
EXTRA_HOUR
EXTRA_MINUTES
EXTRA_MESSAGE
EXTRA_DAYS
ArrayList中
包括每个工作日上应重复此警报。每天必须从一个整数声明
日历
类,如
周一
。
对于一次性报警,不指定该额外费用。
EXTRA_RINGTONE
内容:
URI指定一个铃声与报警,或者使用
VALUE_RINGTONE_SILENT
无铃声。
要使用默认的铃声,不指定该额外费用。
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
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
举例意图过滤器:
<activity ... > <intent-filter> <action android:name = "android.intent.action.SHOW_ALARMS" /> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> </activity>
若要将新事件添加到用户的日历,使用ACTION_INSERT
行动,并指定与数据URI Events.CONTENT_URI
。然后,您可以使用下列定义各种额外活动详情。
ACTION_INSERT
Events.CONTENT_URI
“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
EXTRA_OUTPUT
开放的
对象)。
当相机应用成功返回集中到你的活动(您的应用程序接收到的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
举例意图:
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
举例意图:
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
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
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:
ACTION_PICK
,在上一节中显示(这种方法不需要任何应用程序的权限)。READ_CONTACTS
权限)。ACTION_VIEW
内容:<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:
ACTION_PICK
,在上一节中显示(这种方法不需要任何应用程序的权限)。READ_CONTACTS
权限)。ACTION_EDIT
内容:<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
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
(多个附件)
“text / plain的”
“* / *”
Intent.EXTRA_EMAIL
Intent.EXTRA_CC
Intent.EXTRA_BCC
Intent.EXTRA_SUBJECT
Intent.EXTRA_TEXT
Intent.EXTRA_STREAM
开放的
指向附件。如果使用的是
ACTION_SEND_MULTIPLE
动作,这应该是不是一个
ArrayList中
包含多个
开放的
对象。
举例意图:
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
EXTRA_ALLOW_MULTIPLE
EXTRA_LOCAL_ONLY
CATEGORY_OPENABLE
openFileDescriptor()
。
例如意图得到一张照片:
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" /> <!--可打开类声明,返回的文件是可访问 来自内容提供商supportsOpenableColumns
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
EXTRA_MIME_TYPES
的setType()
,以
“* / *”
。
EXTRA_ALLOW_MULTIPLE
EXTRA_TITLE
ACTION_CREATE_DOCUMENT
到指定初始文件名 。
EXTRA_LOCAL_ONLY
CATEGORY_OPENABLE
openFileDescriptor()
。
例如意图得到一张照片:
只有系统接收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
举例意图:
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
地理:纬度,经度
例如:“地球:47.6,-122.3”
地理:纬度,经度?Z = 缩放
例如:“地球:47.6,-122.3 Z = 11”
地理:0,0 Q =纬度,经度(标签)
例如:“地球:0,0 Q = 34.99,-106.61(宝物)”
地理:0,0 Q =我+街道+地址
例如:“地球:0,0 Q = 1600 +剧场+百汇%2C + CA”
注:在通过所有字符串地理
URI必须进行编码。例如,该串第一和派克,西雅图
应成为 第1%20%26%20Pike%2C%20Seattle
。该字符串中的空格可以与编码 %20
或加号(更换+
)。
举例意图:
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>
HTTP:<网址>
“音频/ *”
“应用程序/ 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
MediaStore.EXTRA_MEDIA_FOCUS
(必填)
指示搜索模式(用户是否正在寻找一个特定的艺术家,专辑,歌曲或播放列表)。大多数搜索模式,采取额外的附加 。例如,如果用户有兴趣听特定的歌曲,其目的可能有三个额外的附加 :歌曲名称,艺术家和专辑。此意向支持的每个值以下搜索模式EXTRA_MEDIA_FOCUS
:
任何 - “vnd.android.cursor.item / *”
播放任何音乐。接收应用程序应发挥基于一个聪明的选择,一些音乐如最后播放列表用户听。
额外的附加:
QUERY
(必需) -空字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。非结构化 - “vnd.android.cursor.item / *”
从非结构化搜索查询播放特定的歌曲,专辑或流派。应用程序可能会产生这种搜索模式时,他们无法识别用户想要收听的内容类型的意图。可能的情况下应用程序应该使用更具体的搜索模式。
额外的附加:
QUERY
(必填) -包含的任意组合的字符串:艺术家,专辑,歌曲名称或流派。流派 - Audio.Genres.ENTRY_CONTENT_TYPE
播放特定类型的音乐。
额外的附加:
“android.intent.extra.genre”
(必需) -流派。QUERY
(必需) -流派。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。艺术家 - Audio.Artists.ENTRY_CONTENT_TYPE
从特定艺术家播放音乐。
额外的附加:
EXTRA_MEDIA_ARTIST
(必需) -艺术家。“android.intent.extra.genre”
-流派。QUERY
(必填) -包含艺术家或流派的任意组合的字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。专辑 - Audio.Albums.ENTRY_CONTENT_TYPE
从一个特定的专辑播放音乐。
额外的附加:
EXTRA_MEDIA_ALBUM
(必需) -专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。QUERY
(必填) -包含专辑或艺术家的任意组合的字符串。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。宋 - “vnd.android.cursor.item /音频”
播放特定的歌曲。
额外的附加:
EXTRA_MEDIA_ALBUM
-专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。EXTRA_MEDIA_TITLE
(必需) -歌曲名。QUERY
(必填) -包含的任意组合的字符串:专辑,艺术家,流派或标题。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。播放列表 - Audio.Playlists.ENTRY_CONTENT_TYPE
播放特定的播放列表,或通过匹配额外附加的一些规定条件的播放列表。
额外的附加:
EXTRA_MEDIA_ALBUM
-专辑。EXTRA_MEDIA_ARTIST
-艺术家。“android.intent.extra.genre”
-流派。“android.intent.extra.playlist”
-播放清单。EXTRA_MEDIA_TITLE
-该播放列表是根据歌曲名称。QUERY
(必填) -包含的任意组合的字符串:专辑,艺术家,流派,播放列表,或称号。这个额外总是提供向后兼容性:现有的应用程序,不知道的搜索模式,可以处理这个意图为非结构化搜索。举例意图:
如果用户想从一个特定的艺术家听音乐,搜索应用程序可能会生成以下意图:
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
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
许可)联系电话:<电话号码>
语音信箱:<电话号码>
有效的电话号码是那些在所定义的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
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
查看设置
为其他设置屏幕可用文档。
举例意图:
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
短信:<PHONE_NUMBER>
smsto:<PHONE_NUMBER>
彩信:<PHONE_NUMBER>
mmsto:<PHONE_NUMBER>
所有这些方案的处理方式相同。
“text / plain的”
“图片/*”
“视频/*”
“学科”
“SMS_BODY”
EXTRA_STREAM
开放的
指向图像或视频附加。如果使用的是
ACTION_SEND_MULTIPLE
动作,这个额外的应该是一个
ArrayList中
的
开放的
指点下到图像/视频附加。
举例意图:
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
HTTP:<网址>
的https:<网址>
“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应用程序,而不是你的网页链接。
要验证您的应用程序响应你想要支持的意图,则可以使用 ADB
工具火特定意图:
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.谷歌现在可以让用户从支持一个给定的动作安装的应用程序选择。
谷歌现在认识到表1中列出有关声明的每个意图过滤器的详细信息的行动声控命令,请单击操作说明。
表1.由谷歌现在公认的语音命令(谷歌搜索应用3.6版)。
类别 | 详细信息和示例 | 操作名称 |
---|---|---|
报警 | 设置闹钟
|
AlarmClock.ACTION_SET_ALARM |
设置计时器
|
AlarmClock.ACTION_SET_TIMER |
|
通讯 | 呼叫号码
|
Intent.ACTION_CALL |
本地 | 预订汽车
|
ReserveIntents |
媒体 | 从搜索播放音乐
|
MediaStore |
拍照
|
MediaStore |
|
录制视频
|
MediaStore |
|
搜索 | 搜索使用某特定应用
|
“com.google.android.gms.actions |
网页浏览器 | 打开网址
|
Intent.ACTION_VIEW |
.