本文主要介紹了隱式Intent匹配目標(biāo)組件的規(guī)則胎挎,若有敘述不清晰或是不準(zhǔn)確的地方希望大家指出杉辙,謝謝大家: )
Intent簡介
Intent用于在一個(gè)組件(Component,如Activity、Service、Broadcast Receiver)中打開另一個(gè)組件掖蛤。可分為隱式(implicitly)和顯式(explicitly)兩種:
- Explicitly Intent:在知道要打開哪個(gè)具體的Component時(shí)使用井厌,通過指定調(diào)用者和被調(diào)用者即可打開目標(biāo)Component蚓庭;
- Implicitly Intent:在不確切的知道要打開哪個(gè)Component的情況下,通過指出action仅仆、data器赞、category,系統(tǒng)會(huì)尋找到匹配的Component蝇恶。
Explicitly Intent
當(dāng)明確知道你想打開哪個(gè)Component時(shí)拳魁,它就是你的菜惶桐。通常這樣使用:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
執(zhí)行以上代碼會(huì)導(dǎo)致目標(biāo)Component(這里是MainActivity)被創(chuàng)建(onCreate(...)等一系列生命周期方法被調(diào)用)撮弧。在MainAcitivity中的相應(yīng)生命周期方法中通過getIntent.getXxxExtra(“key”)即可得到隨Intent一起傳過來的數(shù)據(jù)。
Implicitly Intent
Implicitly Intent很好的實(shí)現(xiàn)了調(diào)用者和被調(diào)用者之間的解耦:調(diào)用者通過action姚糊、data贿衍、category這三個(gè)方面描述他的Intent,被調(diào)用者通過在manifest文件中聲明的一系列Intent Filter來描述自己能夠響應(yīng)哪些意圖救恨。如此一來贸辈,調(diào)用者和被調(diào)用者無需互相了解,通過Implicitly Intent這個(gè)聯(lián)系他們的紐帶就能很好的協(xié)同工作肠槽。
關(guān)于Intent更加詳細(xì)的介紹擎淤,大家可以參考官方文檔或是相關(guān)博文奢啥,這里主要介紹下Implicitly Intent的匹配規(guī)則。
Intent Filter匹配規(guī)則
只有action嘴拢、data桩盲、category三方都匹配,Intent才算是匹配成功席吴,進(jìn)而才能打開相應(yīng)的Component赌结。一個(gè)Component若聲明了多個(gè)Intent Filter,只需要匹配任意一個(gè)即可啟動(dòng)該組件孝冒。*
action的匹配規(guī)則
一個(gè)Intent Filter中可聲明多個(gè)action柬姚,Intent中的action與其中的任一個(gè)action在字符串形式上完全相同(注意,區(qū)分大小寫)庄涡,action方面就匹配成功量承。可通過setAction方法為Intent設(shè)置action穴店,也可在構(gòu)造Intent時(shí)傳入action宴合。需要注意的是,隱式Intent必須指定action(如不指定action則必須指定data或mimetype迹鹅。這種情況下卦洽,只要IntentFilter至少含有一個(gè)action就可以匹配)。比如我們在Manifest文件中為MyActivity定義了如下Intent Filter:
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_TO"/>
</intent-filter>
那么只要Intent的action為“SEND”或“SEND_TO”斜棚,那么這個(gè)Intent在action方面就能和上面那個(gè)Activity匹配成功阀蒂。比如我們的Intent定義如下:
Intent intent = new Intent("android.intent.action.SEND");
那么我們的Intent在action方面就與MyActivity匹配了。
Android系統(tǒng)預(yù)定義了許多action弟蚀,這些action代表了一些常見的操作蚤霞。常見action如下(Intent類中的常量):
Intent.ACTION_VIEW
Intent.ACTION_DIAL
Intent.ACTION_SENDTO
Intent.ACTION_SEND
Intent.ACTION_WEB_SEARCH
data的匹配規(guī)則
data可進(jìn)一步分為uri(由scheme、host义钉、port昧绣、path | pathPattern | pathPrefix這4部分組成)和mimetype。Intent的uri可通過setData方法設(shè)置捶闸,mimetype可通過setType方法設(shè)置夜畴。隱式Intent也必須指定data。同action類似删壮,只要Intent的data只要與Intent Filter中的任一個(gè)data聲明完全相同贪绘,data方面就匹配成功。需要注意的是:若Intent Filter的data聲明部分未指定uri央碟,則缺省uri為content或file税灌,Intent中的uri的scheme部分需為content或file才能匹配;若要為Intent指定完整的data,必須用setDataAndType方法菱涤,原因請看setData和setType方法的源碼:
public Intent setData(Uri data) {
mData = data;
mType = null;
return this;
}
public Intent setType(String type) {
mData = null;
mType = type;
return this;
}
從以上代碼可以看到苞也,setData會(huì)把mimeType置為null,setType會(huì)把uri置為null粘秆。下面我們來舉例說明一下data的匹配墩朦。首先我們先來看一下Intent Filter中指定data的語法:
<data android:scheme="...“
android:host="..."
android:port="..."
android:path="..."
android:pathPattern="..."
android:pathPrefix="..."
android:mimeType="..." />
其中scheme、host等各個(gè)部分無需全部指定翻擒。假如我們?yōu)镸yActivity的Intent Filter指定了如下data:
<intent-filter>
<data android:mimeType="vidoe/mpeg" android:scheme="http" android:host="www.xxx.com" />
<data android:mimeType="text/plain" android:scheme="http" />
</intent-filter>
那么我們的Intent想要匹配氓涣,mimeType可以為”text/plain"或“video/mpeg",scheme必須為”http“,host則沒有限制陋气,因?yàn)榈诙€(gè)data沒有指定host劳吠。
category的匹配規(guī)則
與action和data不同,**Intent中的category必須都在Intent Filter中出現(xiàn)才算匹配成功巩趁。Intent可以不指定category痒玩,若Intent中未指定category,系統(tǒng)會(huì)自動(dòng)為它帶上“android.intent.category.DEFAULT”议慰。所以蠢古,想要接收Implicitly Intent的Component都必須在manifest文件中的Intent Filter聲明中帶上“android.intent.category.DEFAULT”。我們可以通過addCategory方法為Intent添加category别凹。
查詢是否有可接收指定Intent的Component
采用PackageManager的resolveActivity或者Intent的resolveActivity方法會(huì)獲得最適合Intent的一個(gè)Activity草讶;調(diào)用PackageManager的queryIntentActivities會(huì)返回所有成功匹配Intent的Activity。關(guān)于這幾個(gè)方法的詳細(xì)定義大家可以參考官方文檔炉菲,這里不再贅述堕战。
參考資料
1.《Android開發(fā)藝術(shù)探索》
2.Android Docs