Intent用法

在應用中,我們可以以兩種形式來使用Intent:

直接Intent:指定了component屬性的Intent(調用setComponent(ComponentName)或者setClass(Context, Class)來指定)。通過指定具體的組件類,通知應用啓動對應的組件。

間接Intent:沒有指定comonent屬性的Intent。這些Intent需要包含足夠的信息,這樣系統才能根據這些信息,在在所有的可用組件中,確定滿足此Intent的組件。

對於直接Intent,Android不需要去做解析,因爲目標組件已經很明確,Android需要解析的是那些間接Intent,通過解析,將 Intent映射給可以處理此Intent的Activity、IntentReceiver或Service。

Intent解析機制主要是通過查找已註冊在AndroidManifest.xml中的所有IntentFilter及其中定義的Intent, 最終找到匹配的Intent。在這個解析過程中,Android是通過Intent的action、type、category這三個屬性來進行判斷的, 判斷方法如下:

如果Intent指明定了action,則目標組件的IntentFilter的action列表中就必須包含有這個action,否則不能匹配;

如果Intent沒有提供type,系統將從data中得到數據類型。和action一樣,目標組件的數據類型列表中必須包含Intent的數據類型,否則不能匹配。

如果Intent中的數據不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據Intent中數據的scheme (比如 http: 或者mailto: ) 進行匹配。同上,Intent 的scheme必須出現在目標組件的scheme列表中。

如果Intent指定了一個或多個category,這些類別必須全部出現在組建的類別列表中。比如Intent中包含了兩個類別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標組件必須至少包含這兩個類別。

三、應用例子

以下,以Android SDK中的便箋例子來說明,Intent如何定義及如何被解析。這個應用可以讓用戶瀏覽便箋列表、查看每一個便箋的詳細信息。

xml 代碼

複製內容到剪貼板

代碼:

<manifest xmlns:android=http://schemas.android.com/apk/res/android>
<application android:icon="@drawable/app_notes" android:label="@string/app_name">" package="com.google.android.notepad


<provider class="NotePadProvider" android:authorities="com.google.provider.NotePad"/>


<activity  class=".NotesList" android:label="@string/title_notes_list">


<intent-filter>
<action  android:value="android.intent.action.MAIN"/>
<category android:value="android.intent.category.LAUNCHER"/>
</intent-filter>


<intent-filter>
<action android:value="android.intent.action.VIEW"/>
<action android:value="android.intent.action.EDIT"/>
<action android:value="android.intent.action.PICK"/>
<category android:value="android.intent.category.DEFAULT"/>
<type android:value="vnd.android.cursor.dir/vnd.google.note"/>
</intent-filter>


<intent-filter>
<action android:value="android.intent.action.GET_CONTENT"/>
<category android:value="android.intent.category.DEFAULT"/>
<type android:value="vnd.android.cursor.item/vnd.google.note"/>
</intent-filter>


</activity>


<activity class=".NoteEditor" android:label="@string/title_note">


<intent-filter android:label="@string/resolve_edit">
<action android:value="android.intent.action.VIEW"/>
<action android:value="android.intent.action.EDIT"/>
<category android:value="android.intent.category.DEFAULT"/>
<type android:value="vnd.android.cursor.item/vnd.google.note"/>
</intent-filter>


<intent-filter>
<action android:value="android.intent.action.INSERT"/>
<category android:value="android.intent.category.DEFAULT"/>
<type android:value="vnd.android.cursor.dir/vnd.google.note"/>
</intent-filter>


</activity>


<activity class=".TitleEditor"
android:label="@string/title_edit_title"
android:theme="@android:style/Theme.Dialog">


<intent-filter android:label="@string/resolve_title">
<action android:value="com.google.android.notepad.action.EDIT_TITLE"/>
<category android:value="android.intent.category.DEFAULT"/>
<category android:value="android.intent.category.ALTERNATIVE"/>
<category android:value="android.intent.category.SELECTED_ALTERNATIVE"/>
<type android:value="vnd.android.cursor.item/vnd.google.note"/>
</intent-filter>


</activity>


</application>

</manifest>        

例子中的第一個Activity是com.google.android.notepad.NotesList,它是應用的主入口,提供了三個功能,分別由三個 intent-filter進行描述:

1、第一個是進入便箋應用的頂級入口(action爲android.app.action.MAIN)。類型爲android.app.category.LAUNCHER表明這個Activity將在Launcher中列出。

2、第二個是,當type爲vnd.android.cursor.dir/vnd.google.note(保存便箋記錄的目錄)時,可以查看可 用的便箋(action爲android.app.action.VIEW),或者讓用戶選擇一個便箋並返回給調用者(action爲 android.app.action.PICK)。

3、第三個是,當type爲vnd.android.cursor.item/vnd.google.note時,返回給調用者一個用戶選擇的便箋 (action爲android.app.action.GET_CONTENT),而用戶卻不需要知道便箋從哪裏讀取的。有了這些功能,下面的 Intent就會被解析到NotesList這個activity:

複製內容到剪貼板

代碼:
{ action=android.app.action.MAIN }:與此Intent匹配的Activity,將會被當作進入應用的頂級入口。

{ action=android.app.action.MAIN, category=android.app.category.LAUNCHER }:這是目前Launcher實際使用的 Intent,用於生成Launcher的頂級列表。

{ action=android.app.action.VIEW data=content://com.google.provider.NotePad/notes }:

顯示"content://com.google.provider.NotePad/notes"下的所有便箋的列表,使用者可以遍歷列表,並且察看某便箋的詳細信息。

{ action=android.app.action.PICK data=content://com.google.provider.NotePad/notes }:

顯示"content://com.google.provider.NotePad/notes"下的便箋列表,讓用戶可以在列表中選擇一個,然後將選擇的便箋的 URL返回給調用者。

{ action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note }:和上面的action爲pick的Intent類似,不同的是這個Intent允許調用者(在這裏指要調用NotesList的某個 Activity)指定它們需要返回的數據類型,系統會根據這個數據類型查找合適的 Activity(在這裏系統會找到NotesList這個Activity),供用戶選擇便箋。
第二個Activity是com.google.android.notepad.NoteEditor,它爲用戶顯示一條便箋,並且允許 用戶修改這個便箋。

它定義了兩個intent-filter,所以具有兩個功能。

第一個功能是,當數據類型爲 vnd.android.cursor.item/vnd.google.note時,允許用戶查看和修改一個便籤(action爲 android.app.action.VIEW和android.app.action.EDIT)。

第二個功能是,當數據類型爲 vnd.android.cursor.dir/vnd.google.note,爲調用者顯示一個新建便箋的界面,並將新建的便箋插入到便箋列表中(action爲android.app.action.INSERT)。

有了這兩個功能,下面的Intent就會被解析到NoteEditor這個activity:

複製內容到剪貼板

代碼:
{ action=android.app.action.VIEW data=content://com.google.provider.NotePad/notes/{ID}} :向用戶顯示標識爲 ID的便箋。

{ action=android.app.action.EDIT data=content://com.google.provider.NotePad/notes/{ID}}:允許用戶編輯標識爲ID的便箋。

{ action=android.app.action.INSERT data=content://com.google.provider.NotePad/notes }:在“content://com.google.provider.NotePad/notes”這個便箋列表中創建一個新的空便箋,並允許用戶編輯 這個便籤。當用戶保存這個便箋後,這個新便箋的URI將會返回給調用者。
最後一個Activity是com.google.android.notepad.TitleEditor,它允許用戶編輯便箋的標題。

它可以被實現爲一個應用可以直接調用(在Intent中明確設置component屬性)的類,不過這裏我們將爲你提供一個在現有的數據上發佈可選操作的方法。

在這個 Activity的唯一的intent-filter中,擁有一個私有的action: com.google.android.notepad.action.EDIT_TITLE,表明允許用戶編輯便箋的標題。

和前面的view和edit 動作一樣,調用這個Intent 的時候,也必須指定具體的便箋(type爲vnd.android.cursor.item/vnd.google.note)。不同的是,這裏顯示和編輯的只是便箋數據中的標題。

除了支持缺省類別(android.intent.category.DEFAULT),標題編輯器還支持另外兩個標準類別: android.intent.category.ALTERNATIVE和
android.intent.category.SELECTED_ALTERNATIVE。

實現了這兩個類別之後,其它 Activity就可以調用queryIntentActivityOptions(ComponentName, Intent[], Intent, int)查詢這個Activity提供的action,而不需要了解它的具體實現;

或者調用addIntentOptions(int, int, ComponentName, Intent[], Intent, int, Menu.Item[])建立動態菜單。需要說明的是,在這個intent-filter中有一個明確的名稱(通過android:label= "@string/resolve_title"指定),在用戶瀏覽數據的時候,如果這個Activity是數據的一個可選操作,指定明確的名稱可以爲用 戶提供一個更好控制界面。

有了這個功能,下面的Intent就會被解析到TitleEditor這個Activity:

複製內容到剪貼板
代碼:
{ action=com.google.android.notepad.action.EDIT_TITLE data=content://com.google.provider.NotePad/notes/{ID}}:顯示並且允許用戶編輯標識爲ID的便 箋的標題。

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