startActivity or startActivityForResults
startService or bindService
sendBroadcast, sendOrderedBroadcast or sendStickyBroadcast
set Component(), setClass(), setClassName()
, 或者是做爲Intent()的構造器的參數。若沒指定該參數,則系統使用一下item進行匹配。其中Service必須指定Component Name。static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";
content: URI
的URI數據,可不指定類型,由於系統可識別出該數據位於設備上,而且受到ContentProvider控制,所以MIME類型就變爲可見。setData(), setType() or setDataAndType()
,其中setData和setType不可同時使用,若須要,請使用setDataAndType()。android.intent.category.DEFAULT
。常見的Category:CATEGORY_BROWSABLE
和CATEGORY_LAUNCHER
。前者表示該數據可爲瀏覽器打開,後者表示該Activity是一個任務的初始Activity,而且將會被顯示在Launcher裏。可用'addCategory()'來指定該項。static final String EXTRA_GIGAWATTS = "com.example.EXTRA_GIGAWATTS";
// Executed in an Activity, so 'this' is the Context // The fileUrl is a string URL, such as "http://www.example.com/image.png" Intent downloadIntent = new Intent(this, DownloadService.class); downloadIntent.setData(Uri.parse(fileUrl)); startService(downloadIntent);
// Create the text message with a string Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type // Verify that the intent will resolve to an activity if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); }
如有多個App響應,則顯示列表對話框以供用戶選擇,若是隻有一個App響應,則直接啓動。
- 強制出現App選擇器
如分享文件或者數據到各類App,ACTION_SEND。可以使用Intent的creatChooser來操做,以下:html
Intent intent = new Intent(Intent.ACTION_SEND); ... // Always use string resources for UI text. // This says something like "Share this photo with" String title = getResources().getString(R.string.chooser_title); // Create intent to show chooser Intent chooser = Intent.createChooser(intent, title); // Verify the intent will resolve to at least one activity if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); }
在Manifest文件中申明< intent-filters >標籤,經過action, data, and category等字段制定Intent可接受的類型。
一個Explicit Intent無視組件所申明的接受Intent類型,直接傳遞過去。
不一樣job應申明不一樣的< intent-filters >,可包括action, data, and category3個字段。
-action, 指定可接受的intetn action。
-data,可接受的數據類型,URI (scheme, host, port, path, etc.) and MIME type。
-category,必須是一個Action的string值。android
Tip: 爲了收到Implicit Intents,必須設定
CATEGORY_DEFAULT
。這樣, startActivity() and startActivityForResult() 纔可調用,不然沒有任何Implicit可啓動該Activity。算法
舉例:shell
<activity android:name="ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity>
只有三個item都經過驗證,Android纔會傳遞Intent給該Activity。對於Broadcast Receiver,可經過registerReceiver()
or unregisterReceiver()
動態綁定。
- Example Filters瀏覽器
<activity android:name="MainActivity"> <!-- This activity is the main entry, should appear in app launcher --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="ShareActivity"> <!-- This activity handles "SEND" actions with text data --> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data --> <intent-filter> <action android:name="android.intent.action.SEND"/> <action android:name="android.intent.action.SEND_MULTIPLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="application/vnd.google.panorama360+jpg"/> <data android:mimeType="image/*"/> <data android:mimeType="video/*"/> </intent-filter> </activity>
它是對Intent的包裝器,主要用途是給予其餘App權限獲取本進程的數據。主要用途以下:網絡
因爲每一個Intent都是爲特定的組件設計的,所以PendingIntent也一樣。app
Android 系統匹配最優的組件的算法,基於如下三方面的匹配測試:ide
<scheme>://<host>:<port>/<path>
content://com.example.project:200/folder/subfolder/etc
PackageManager 能夠調用如下幾個方法列出可接受該Intent的組件post
Action: ACTION_SET_ALARM
Extras:....
ex:測試
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); } }
並其須要使用權限:<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
相應的Intent-Filter
<activity ...> <intent-filter> <action android:name="android.intent.action.SET_ALARM" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Action:ACTION_SET_TIMER
Extras:。。。
e.g.
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); } }
Permission:<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
Intent-Filter:
<activity ...> <intent-filter> <action android:name="android.intent.action.SET_TIMER" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Action:ACTION_SHOW_ALARMS
Action:ACTION_INSERT
Data URI:Events.CONTENT_URI
MIME Type: "vnd.android.cursor.dir/event"
Extras: 。。。
Action:ACTION_IMAGE_CAPTURE or ACTION_VIDEO_CAPTURE
Extras:EXTRA_OUTPUT
使用onActivityResult()
接收返回數據:
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 ... } }
相應的Intent-Filter
<activity ...> <intent-filter> <action android:name="android.media.action.IMAGE_CAPTURE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Action: INTENT_ACTION_STILL_IMAGE_CAMERA
Action: INTENT_ACTION_VIDEO_CAMERA
Action:ACTION_PICK
MIME Type:Contacts.CONTENT_TYPE
e.g.:
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 ... } }
Action: ACTION_PICK
MIME Type:
CommonDataKinds.Phone.CONTENT_TYPE。Pick from contacts with a phone number.
CommonDataKinds.Email.CONTENT_TYPE,Pick from contacts with an email address.
CommonDataKinds.StructuredPostal.CONTENT_TYPE,Pick from contacts with a postal address.
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 ... } } }
public void viewContact(Uri contactUri) { Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
Action:ACTION_EDIT
URI: 以前ACTION_PICK返回的contactURI。
Extras: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:ACTION_INSERT
MIME Type:Contacts.CONTENT_TYPE
Extras:One or more of the extras defined in ContactsContract.Intents.Insert.
e.g.:
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: ACTION_VIEW
URI: geo:latitude,longtitude
e.g.:
public void showMap(Uri geoLocation) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(geoLocation); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
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>
Action: ACTION_VIEW
Data URI Scheme:
MIME Type:
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>
Action: ACTION_DIAL , ACTION_CALL
其中ACTION_DIAL,須要傳輸一個號碼,而後跳轉到撥打界面,須要點擊撥打按鈕。
而ACTION_CALL,須要傳輸一個號碼,而後直接開始撥打,可是須要權限<uses-permission android:name="android.permission.CALL_PHONE" />
Data URI Scheme:'tel:< phone-number>,voicemail:< phone-number>'
e.g.:
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); } }
Action:
Action: ACTION_SENDTO, ACTION_SEND, ACTION_SEND_MULTIPLE
Data URI Scheme:
sms:<phone_number> smsto:<phone_number> mms:<phone_number> mmsto:<phone_number>
MIME Type:
PLAIN_TEXT_TYPE ("text/plain")
"image/"
"video/"
Extras:
"subject",'sms_body','EXTRA_STREAM'
e.g.:
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); } }
若想要確保intent被短信應用處理,使用 ACTION_SENDTO,而且設置data schmem爲‘smsto:’.
相應的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>
加載網址
- Action: ACTION_VIEW
- Data URI Scheme:
http:< URL>
https:< URL>
- MIME Type:
PLAIN_TEXT_TYPE ("text/plain")
"text/html"
"application/xhtml+xml"
"application/vnd.wap.xhtml+xml"
Web查詢
Action: ACTION_WEB_SEARCH
Extras:SearchManager.QUERY The search string.
public void searchWeb(String query) { Intent intent = new Intent(Intent.ACTION_SEARCH); intent.putExtra(SearchManager.QUERY, query); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } }
adb shell am start -a <ACTION> -t <MIME_TYPE> -d <DATA> \ -e <EXTRA_NAME> <EXTRA_VALUE> -n <ACTIVITY>
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.