Android開發-——-Intent-filter屬性詳解

(看到別人在Menifest中註冊的anctivity的一些屬性不是很瞭解(如下),所以就又回顧、學習了一下。

<activity
            android:name="com.project.activity.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" >
            <intent-filter>
                <data android:scheme="tencent100371282" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>    
 下面是詳細內容

如果一個 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 )。

接下來的代碼片段顯示瞭如何配置 Activity 的 Intent Filter ,使其以在特定數據下的默認的或可替代的動作的身份來執行 SHOW_DAMAGE動作.

[html] view plaincopy
  1. Java代碼  
  2.     
  3.  <activity android:name=".EarthquakeDamageViewer"   
  4.    android:label="View Damage">   
  5.      
  6.    <intent-filter>   
  7.      
  8.    <action   
  9.       
  10.    android:name="com.paad.earthquake.intent.action.SHOW_DAMAGE">   
  11.      
  12.   </action>   
  13.      
  14.   <category android:name="android.intent.category.DEFAULT"/>   
  15.      
  16.    <category   
  17.     
  18.    android:name="android.intent.category.ALTERNATIVE_SELECTED"  />   
  19.    
  20.   <data android:mimeType="vnd.earthquake.cursor.item/*"/>   
  21.     
  22.   </intent-filter>   
  23.     
  24.   </activity>   
  25. <activity android:name=".EarthquakeDamageViewer"  
  26. android:label="View Damage">  
  27. <intent-filter>  
  28. <action  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章