Android 隐式意图的配置
本文地址:http://www.cnblogs.com/wuyudong/p/5677473.html,轉載請注明源地址。
《Android 顯示意圖激活另外一個Actitity》一文介紹了一種激活Activity的方法
本文通過清單文件(AndroidManifest.xml)來實現意圖的配置
Intent-filter屬性詳解
如果一個 Intent 請求在一片數據上執行一個動作, Android 如何知道哪個應用程序(和組件)能用來響應這個請求呢??
Intent Filter就是用來注冊 Activity 、 Service 和 Broadcast Receiver 具有能在某種數據上執行一個動作的能力。
使用 Intent Filter ,應用程序組件告訴 Android ,它們能為其它程序的組件的動作請求提供服務,包括同一個程序的組件、本地的或第三方的應用程序。
為了注冊一個應用程序組件為 Intent 處理者,在組件的 manifest 節點添加一個 intent-filter 標簽。
在 Intent Filter 節點里使用下面的標簽(關聯屬性),你能指定組件支持的動作、種類和數據:
1.動作測試
<intent-filter>元素中可以包括子元素<action>,比如:
<intent-filter ><action android:name="com.example.project.SHOW_CURRENT" /><action android:name="com.example.project.SHOW_RECENT" /><action android:name="com.example.project.SHOW_PENDING" /> </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>子元素,比如:
<intent-filter . . . ><category android:name="android.Intent.Category.DEFAULT" /><category android:name="android.Intent.Category.BROWSABLE" /> </intent-filter >只有當Intent請求中所有的Category與組件中某一個IntentFilter的<category>完全匹配時,才會讓該 Intent請求通過測試,IntentFilter中多余的<category>聲明并不會導致匹配失敗。一個沒有指定任何類別測試的 IntentFilter僅僅只會匹配沒有設置類別的Intent請求。
3.數據測試
數據在<intent-filter>中的描述如下:
<intent-filter . . . ><data android:type="video/mpeg" android:scheme="http" . . . /><data android:type="audio/mpeg" android:scheme="http" . . . /> </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 )。
實戰一下
添加《Android 顯示意圖激活另外一個Actitity》中的跳轉功能,實現隱式意圖激活第三個界面
代碼:
// 采用隱式意圖激活第三個界面public void click3(View view) {Intent intent = new Intent();intent.setAction("com.wuyudong.xxx");intent.addCategory("android.intent.category.DEFAULT");//intent.setData(Uri.parse("wuyudong:hahaha"));//指定數據類型//intent.setType("vnd.android.cursor.item/xixixi"); intent.setDataAndType(Uri.parse("wuyudong:hahaha"), "vnd.android.cursor.item/xixixi");startActivity(intent);}可以在OtherScreenActivity接收數據,代碼如下:
public class OtherScreenActivity extends Activity {//重寫activity的onCreate方法 方法里面設置初始化程序的界面 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_two);Intent intent = getIntent(); //獲取到激活它的意圖Uri uri = intent.getData();System.out.println(uri.getScheme());} }清單文件代碼如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.wuyudong.twoactivity"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:icon="@drawable/icon1"android:name="com.wuyudong.twoactivity.MainActivity"android:label="@string/activity01" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:icon="@drawable/icon2"android:name="com.wuyudong.twoactivity.OtherScreenActivity"android:label="@string/activity02" ><intent-filter><action android:name="com.wuyudong.xxx"/><data android:scheme="wuyudong" android:mimeType="vnd.android.cursor.item/xixixi"></data><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity></application></manifest>總結
以上是生活随笔為你收集整理的Android 隐式意图的配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 住宅的结构有哪些形式
- 下一篇: 面试要求 熟悉linux系统,Linux