intent Filter,intentfilter
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
二、代碼執行個體
程式執行個體
點擊後
com.fry.intent_Filter2.MainActivity
1 package com.fry.intent_Filter2; 2 3 4 5 import com.example.activity.R; 6 7 import android.app.Activity; 8 import android.content.Intent; 9 import android.net.Uri;10 import android.os.Bundle;11 import android.view.View;12 import android.view.View.OnClickListener;13 import android.widget.Button;14 15 16 17 public class MainActivity extends Activity{18 private Button btn_openActivty;//建立一個button對象19 protected void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);//父類操作21 setContentView(R.layout.activity_main);//引入名為activity_main的介面22 btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id為btn_openActivity的button23 btn_openActivty.setOnClickListener(new OnClickListener() {//設定button點擊監聽24 25 @Override26 public void onClick(View v) {//onclick事件27 // TODO Auto-generated method stub28 Intent intent=new Intent();//初始化intent29 /**30 * 找所有action符合為"com.fry.activity01"的頁面31 * 也找所有Category為android.intent.category.DEFAULT的頁面32 * String android.content.Intent.CATEGORY_DEFAULT = 33 * "android.intent.category.DEFAULT"34 */35 intent.setAction("com.fry.activity01");36 intent.addCategory(Intent.CATEGORY_DEFAULT);37 //當只有data沒有type的時候38 //intent.setData(Uri.parse("http://www.baidu.com:8080/image"));39 intent.setDataAndType(Uri.parse("http://www.baidu.com:8080/image"), "text/plain");40 startActivity(intent);//開啟activity41 }42 });43 }44 }
/intent_Filter2/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activity" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application11 android:allowBackup="true"12 android:icon="@drawable/ic_launcher"13 android:label="@string/app_name"14 android:theme="@style/AppTheme" >15 <activity16 android:name="com.fry.intent_Filter2.MainActivity"17 android:label="@string/app_name" >18 <intent-filter>19 <action android:name="android.intent.action.MAIN" />20 21 <category android:name="android.intent.category.LAUNCHER" />22 </intent-filter>23 </activity>24 <activity android:name="com.fry.intent_Filter2.Activity01">25 <intent-filter >26 <action android:name="com.fry.activity01"></action>27 <category android:name="android.intent.category.DEFAULT"/>28 <data android:scheme="http" android:host="www.baidu.com" android:port="8080" android:path="/image" android:mimeType="text/plain"></data>29 </intent-filter>30 </activity>31 </application>32 33 </manifest>