1. 程式人生 > >intent Filter

intent Filter

images 部分 port aud style 失敗 filter pri 登陸

intent Filter

一、介紹

技術分享

如果一個 Intent 請求在一片數據上執行一個動作, Android 如何知道哪個應用程序(和組件)能用來響應這個請求呢?

Intent Filter就是 用來註冊 Activity 、 Service 和 Broadcast Receiver 具有能在某種數據上執行一個動作的能力。
使用 Intent Filter ,應用程序組件告訴 Android ,它們能為其它程序的組件的動作請求提供服務,包括同一個程序的組
件、本地的或第三方的應用程序。

為了註冊一個應用程序組件為 Intent 處理者,在組件的 manifest 節點添加一個 intent-filter 標簽。

在 Intent Filter 節點裏使用下面的標簽(關聯屬性),你能指定組件支持的動作、種類和數據:
1.動作測試


<intent-filter>元素中可以包括子元素<action>,比如:
view source print ?
1. < intent-filter >
2. < action android:name="com.example.project.SHOW_CURRENT" />
3. < action android:name="com.example.project.SHOW_RECENT" />
4. < action android:name="com.example.project.SHOW_PENDING" />
5. </ intent-filter >
一條<intent-filter>元素至少應該包含一個<action>,否則任何Intent請求都不能和該<intent-filter>匹配。如果Intent

請求的Action和<intent-filter>中個某一條<action>匹配,那麽該Intent就通過了這條<intent-filter>的動作測試。如果
Intent請求或<intent-filter>中沒有說明具體的Action類型,那麽會出現下面兩種情況。
(1) 如果<intent-filter>中沒有包含任何Action類型,那麽無論什麽Intent請求都無法和這條<intent-filter>匹配;
(2) 反之,如果Intent請求中沒有設定Action類型,那麽只要<intent-filter>中包含有Action類型,這個Intent請求就將順
利地通過<intent-filter>的行為測試。

2.類別測試

<intent-filter>元素可以包含<category>子元素,比如:
view source print ?
1. < intent-filter . . . >
2. < category android:name="android.Intent.Category.DEFAULT" />
3. < category android:name="android.Intent.Category.BROWSABLE" />
4. </ intent-filter >
只有當Intent請求中所有的Category與組件中某一個IntentFilter的<category>完全匹配時,才會讓該 Intent請求通過測試
IntentFilter中多余的<category>聲明並不會導致匹配失敗。一個沒有指定任何類別測試的 IntentFilter僅僅只會匹配沒
有設置類別的Intent請求。

3.數據測試

數據在<intent-filter>中的描述如下:
view source print ?
1. < intent-filter . . . >
2. < data android:type="video/mpeg" android:scheme="http" . . . />
3. < data android:type="audio/mpeg" android:scheme="http" . . . />
4. </ intent-filter >
<data>元素指定了希望接受的Intent請求的數據URI和數據類型,URI被分成三部分來進行匹配:scheme、 authority和path
。其中,用setData()設定的Inteat請求的URI數據類型和scheme必須與IntentFilter中所指定的一致。若IntentFilter中還指定了authority或path,它們也需要相匹配才會通過測試。

? action
使用 android:name 特性來指定對響應的動作名。動作名必須是獨一無二的字符串,所以,一個好的習慣是使用基於 Java 包的命名方式的命名系統。

? category

使用 Android:category 屬性用來指定在什麽樣的環境下動作才被響應。每個 Intent Filter 標簽可以包含多個 category 標簽。你可以指定自定義的種類或使用 android 提供的標準值,如下所示:

? ALTERNATIVE
你將在這章的後面所看到的,一個 Intent Filter 的用途是使用動作來幫忙填入上下文菜單。 ALTERNATIVE 種類指定,在某種數據類型的項目上可以替代默認執行的動作。例如,一個聯系人的默認動作時瀏覽它,替代的可能是去編輯或刪除它。

? SELECTED_ALTERNATIVE
與 ALTERNATIVE 類似,但 ALTERNATIVE 總是使用下面所述的 Intent 解析來指向單一的動作。SELECTED_ALTERNATIVE在需要一個可能性列表時使用。

? BROWSABLE
指定在瀏覽器中的動作。當 Intent 在瀏覽器中被引發,都會被指定成 BROWSABLE 種類。

? DEFAULT
設置這個種類來讓組件成為 Intent Filter 中定義的 data 的默認動作。這對使用顯式 Intent 啟動的 Activity 來說也是必要的。

? GADGET
通過設置 GADGET 種類,你可以指定這個 Activity 可以嵌入到其他的 Activity 來允許。

? HOME
HOME Activity 是設備啟動(登陸屏幕)時顯示的第一個 Activity 。通過指定 Intent Filter 為 HOME 種類而不指定動作的話,你正在將其設為本地 home 畫面的替代。

? LAUNCHER
使用這個種類來讓一個 Activity 作為應用程序的啟動項。

? data
data 標簽允許你指定組件能作用的數據的匹配;如果你的組件能處理多個的話,你可以包含多個條件。你可以使用下面屬性的任意組合來指定組件支持的數據:

? android:host
指定一個有效的主機名(例如, com.google )。

? android:mimetype
允許你設定組件能處理的數據類型。例如,<type android:value=”vnd.android.cursor.dir/*”/>能匹配任何 Android 遊標。

? android:path
有效地 URI 路徑值(例如, /transport/boats/ )。

? android:port
特定主機上的有效端口。

? android:scheme
需要一個特殊的圖示(例如, content 或 http )。

參考:http://blog.csdn.net/wuwenxiang91322/article/details/7671593

二、代碼實例

intent Filter