欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中的Intent Filter匹配規(guī)則簡(jiǎn)介

 更新時(shí)間:2016年04月12日 16:07:14   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了Android中的Intent Filter匹配規(guī)則,感興趣的小伙伴們可以參考一下

本文主要介紹了隱式Intent匹配目標(biāo)組件的規(guī)則,若有敘述不清晰或是不準(zhǔn)確的地方希望大家指出,謝謝大家:  )

1. Intent簡(jiǎn)介

Intent用于在一個(gè)組件(Component,如Activity、Service、Broadcast Receiver)中打開(kāi)另一個(gè)組件。

Intent可分為隱式(implicitly)和顯式(explicitly)兩種:

Explicitly Intent:在知道要打開(kāi)哪個(gè)具體的Component時(shí)使用,通過(guò)指定調(diào)用者和被調(diào)用者即可打開(kāi)目標(biāo)Component;
Implicitly Intent:在不確切的知道要打開(kāi)哪個(gè)Component的情況下,通過(guò)指出action、data、category,系統(tǒng)會(huì)尋找到匹配的Component。
(1)Explicitly Intent

當(dāng)明確知道你想打開(kāi)哪個(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)生命周期方法中通過(guò)getIntent.getXxxExtra(“key”)即可得到隨Intent一起傳過(guò)來(lái)的數(shù)據(jù)。

(2)Implicitly Intent

Implicitly Intent很好的實(shí)現(xiàn)了調(diào)用者和被調(diào)用者之間的解耦:

調(diào)用者通過(guò)action、data、category這三個(gè)方面描述他的Intent,被調(diào)用者通過(guò)在manifest文件中聲明的一系列Intent Filter來(lái)描述自己能夠響應(yīng)哪些意圖。如此一來(lái),調(diào)用者和被調(diào)用者無(wú)需互相了解,通過(guò)Implicitly Intent這個(gè)聯(lián)系他們的紐帶就能很好的協(xié)同工作。

關(guān)于Intent更加詳細(xì)的介紹,大家可以參考官方文檔,這里主要介紹下Implicitly Intent的匹配規(guī)則。

2.Intent Filter匹配規(guī)則

只有action、data、category三方都匹配,Intent才算是匹配成功,進(jìn)而才能打開(kāi)相應(yīng)的Component。一個(gè)Component若聲明了多個(gè)Intent Filter,只需要匹配任意一個(gè)即可啟動(dòng)該組件。

(1)action的匹配規(guī)則

一個(gè)Intent Filter中可聲明多個(gè)action,Intent中的action與其中的任一個(gè)action在字符串形式上完全相同(注意,區(qū)分大小寫(xiě)),action方面就匹配成功??赏ㄟ^(guò)setAction方法為Intent設(shè)置action,也可在構(gòu)造Intent時(shí)傳入action。需要注意的是,隱式Intent必須指定action。比如我們?cè)贛anifest文件中為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代表了一些常見(jiàn)的操作。常見(jiàn)action如下(Intent類中的常量):

Intent.ACTION_VIEW
Intent.ACTION_DIAL
Intent.ACTION_SENDTO
Intent.ACTION_SEND
Intent.ACTION_WEB_SEARCH

(2)data的匹配規(guī)則

data可進(jìn)一步分為uri(由scheme、host、port、path | pathPattern | pathPrefix這4部分組成)和mimetype。Intent的uri可通過(guò)setData方法設(shè)置,mimetype可通過(guò)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方法,原因請(qǐng)看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。下面我們來(lái)舉例說(shuō)明一下data的匹配。首先我們先來(lái)看一下Intent Filter中指定data的語(yǔ)法:

<data android:scheme="...“ 
     android:host="..."
     android:port="..."
     android:path="..."
     android:pathPattern="..."
     android:pathPrefix="..."
     android:mimeType="..." />

其中scheme、host等各個(gè)部分無(wú)需全部指定。假如我們?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則沒(méi)有限制,因?yàn)榈诙€(gè)data沒(méi)有指定host。

(3)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”。我們可以通過(guò)addCategory方法為Intent添加category。

(4)查詢是否有可接收指定Intent的Component

采用PackageManager的resolveActivity或者Intent的resolveActivity方法會(huì)獲得最適合Intent的一個(gè)Activity;調(diào)用PackageManager的queryIntentActivities會(huì)返回所有成功匹配Intent的Activity。關(guān)于這幾個(gè)方法的詳細(xì)定義大家可以參考官方文檔,這里不再贅述。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論