Intent是一個(gè)用來(lái)從一個(gè)App組件啟動(dòng)其他組件的消息對(duì)象。這里有3種基本的用法:
- 啟動(dòng)activity: 傳遞Intent,使用startActivity()或者startActivityForResult(),后者可以傳回結(jié)果。
- 啟動(dòng)service: 傳遞Intent,使用startService()啟動(dòng)一個(gè)沒(méi)有用戶界面的service,如果service被設(shè)計(jì)為C/S結(jié)構(gòu),可以使用bindService()綁定一個(gè)service。
- 傳遞broadcast: 可以向其他app傳遞信息通過(guò)Intent對(duì)象使用sendBroadcast()、 sendOrderedBroadcast()或sendStickyBroadcast()。
Intent類型
- 顯式Intent 顯式的Intent類型就是在啟動(dòng)的時(shí)候指定組件的類型,這個(gè)一般適用于自己的App。
- 隱式Intent 隱式的Intent類型一般是指指明action,然后傳入Intent,適用于非己App。
當(dāng)你創(chuàng)建一個(gè)隱式的Intent對(duì)象,安卓系統(tǒng)會(huì)通過(guò)Intent過(guò)濾器查找manifest文件尋找合適的App打開(kāi)之。如果很多應(yīng)用都有action,那么會(huì)出現(xiàn)一個(gè)對(duì)話框,讓用戶選擇想要的程序。
下面這張圖顯示了一個(gè)Activity如何通過(guò)Intent來(lái)打開(kāi)另外一個(gè)Activity。
注意:為了保證你的app的安全性,請(qǐng)一直使用顯式的Intent來(lái)開(kāi)啟一個(gè)service,并且不要打開(kāi)Intent過(guò)濾器,因?yàn)橥ㄟ^(guò)過(guò)濾器打開(kāi)service不能保證啟動(dòng)者的身份。
建立一個(gè)Intent
Intent對(duì)象包含了安卓系統(tǒng)決定開(kāi)啟那些應(yīng)用的信息并且包含了一些傳遞信息。Intent對(duì)象包含了以下內(nèi)容:
Component name: 要開(kāi)啟的組件的名稱。顯式Intent所必須的,如果沒(méi)有該項(xiàng),則為隱式Intent。對(duì)于service必須指定該項(xiàng)。你可以通過(guò) setComponent(), setClass(), setClassName()等函數(shù)設(shè)置該項(xiàng)的內(nèi)容,比如com.example.ExampleActivity
Action 一個(gè)指定特定的行為的字符串,比如view、pick。
你可以使用setAction()函數(shù)來(lái)指定Intent的Action,也可以通過(guò)以方式自己定義action:
static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";
Data: URI對(duì)象可以作為MIME類型的數(shù)據(jù)。數(shù)據(jù)的類型由Intent的Action決定,例如,如果動(dòng)作是ACTION_EDIT ,數(shù)據(jù)應(yīng)包含編輯文檔的URI。
設(shè)置Data URI需要調(diào)用setData()函數(shù),如果僅僅設(shè)置MIME類型,則需要調(diào)用setType()函數(shù),如果有必要,你可以同時(shí)設(shè)置兩者通過(guò)調(diào)用setDataAndType()函數(shù)(這種情況下setData()、setType()函數(shù)將不起作用)。
Category:這個(gè)內(nèi)容包含了額外的信息,這些信息需要下一個(gè)組件處理相關(guān)的信息,不是必要的。這兒有一些例子:
CATEGORY_BROWSABLE 目標(biāo)Activity允許自身通過(guò)web瀏覽器來(lái)顯示由鏈接諸如圖像或電子郵件消息中引用的數(shù)據(jù)來(lái)啟動(dòng)。
CATEGORY_LAUNCHER 這個(gè)Activity是系統(tǒng)app啟動(dòng)器中的任務(wù)欄里啟動(dòng)的Activity。
你可以通過(guò)調(diào)用addCategory()函數(shù)來(lái)指定category。
Extras:鍵值對(duì)保存的信息。可以通過(guò)putExtra()存入,也可以建立一個(gè)Bundle對(duì)象保存所有的Extra數(shù)據(jù),然后通過(guò)putExtras()函數(shù)插入到Intent中。
Flags : Intent類中定義了Flags函數(shù)作為元數(shù)據(jù)。這個(gè)參數(shù)可以禁止安卓系統(tǒng)如何運(yùn)行activity并且接下來(lái)如何執(zhí)行。相關(guān)函數(shù)為setFlags()
一個(gè)例子:
// 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);
這個(gè)例子開(kāi)啟了一個(gè)下載服務(wù),并將傳入的fileUrl內(nèi)容下載下來(lái)。
再來(lái)一個(gè)例子:
// 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);
}
這個(gè)例子分享了一些文字信息。
強(qiáng)制使用App選擇器
有時(shí)候需要不直接打開(kāi)默認(rèn)應(yīng)用,而是每次都要求用戶選擇打開(kāi)的app,這可以創(chuàng)建一個(gè)createChooser() Intent對(duì)象,然后將其傳入startActivity(),如下:
Intent sendIntent = 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 the chooser dialog
Intent chooser = Intent.createChooser(sendIntent, title);
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
接收一個(gè)隱式的Intent
需要使用intent-filter,前面有說(shuō),就不廢話了,直接上例子:
<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>
這是一個(gè)多意圖的過(guò)濾器。第一個(gè)Activity,MainActivity,是app的主要入口,通過(guò)點(diǎn)擊圖標(biāo)進(jìn)入,這個(gè)是由"android.intent.action.MAIN"決定的。 "android.intent.category.LAUNCHER"決定這個(gè)Activity是在系統(tǒng)app啟動(dòng)器中啟動(dòng)的。如果沒(méi)有指定icon屬性,則圖標(biāo)為默認(rèn)。
第二個(gè)Activity,ShareActivity,為了便于共享文本和媒體內(nèi)容,可以通過(guò)ShareActivity從其他app中進(jìn)入,當(dāng)然需要通過(guò)過(guò)濾器。
如果你想只有自己訪問(wèn)自己的過(guò)濾器而不是讓其他app訪問(wèn)的話,可以設(shè)置exported屬性為false。
使用Pending Intent
pending Intent是Intent的一種包裝,主要的目的是將權(quán)限授予其他程序申請(qǐng)使用包含的Intent,就像執(zhí)行自己的程序一樣。主要的用途包括:
- 申明一個(gè)Intent,當(dāng)用戶平臺(tái)接收到你的通知后執(zhí)行。
- 申明一個(gè)Intent,當(dāng)用戶使用widget時(shí)執(zhí)行。
- 申明一個(gè)Intent,在未來(lái)規(guī)定的時(shí)間內(nèi)執(zhí)行。
這里有三個(gè)方法:
- PendingIntent.getActivity() for an Intent that starts an Activity.
- PendingIntent.getService() for an Intent that starts a Service.
- PendingIntent.getBroadcast() for a Intent that starts an BroadcastReceiver.
具體的pending Intent官方解釋在這兒。
Intent解決方案
當(dāng)系統(tǒng)接收到一個(gè)隱式的Intent后,會(huì)通過(guò)action、data、category決定使用哪個(gè)app。
Action test
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.VIEW" />
...
</intent-filter>
Category test
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
...
</intent-filter>
Data test
<intent-filter>
<data android:mimeType="video/mpeg" android:scheme="http" ... />
<data android:mimeType="audio/mpeg" android:scheme="http" ... />
...
</intent-filter>
Intent匹配
queryIntentActivities()可以返回那些組件可以接受你的Intent,具體請(qǐng)查看這里
P.S:每天寫一篇感覺(jué)好累啊……