Android如何解析Intent
在應用中,我們可以以兩種形式來使用Intent:
直接Intent:指定了component屬性的Intent(調(diào)用setComponent(ComponentName)或者setClass(Context, Class)來指定)。通過指定具體的組件類,通知應用啟動對應的組件。
間接Intent:沒有指定comonent屬性的Intent。這些Intent需要包含足夠的信息,這樣系統(tǒng)才能根據(jù)這些信息,在在所有的可用組件中,確定滿足此Intent的組件。
對于直接Intent,Android不需要去做解析,因為目標組件已經(jīng)很明確,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,系統(tǒng)將從data中得到數(shù)據(jù)類型。和action一樣,目標組件的數(shù)據(jù)類型列表中必須包含Intent的數(shù)據(jù)類型,否則不能匹配。
如果Intent中的數(shù)據(jù)不是content: 類型的URI,而且Intent也沒有明確指定它的type,將根據(jù)Intent中數(shù)據(jù)的scheme (比如 http: 或者mailto: ) 進行匹配。同上,Intent 的scheme必須出現(xiàn)在目標組件的scheme列表中。
如果Intent指定了一個或多個category,這些類別必須全部出現(xiàn)在組建的類別列表中。比如Intent中包含了兩個類別:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目標組件必須至少包含這兩個類別。
三、應用例子
以下,以Android SDK中的便箋例子來說明,Intent如何定義及如何被解析。這個應用可以讓用戶瀏覽便箋列表、查看每一個便箋的詳細信息。
xml 代碼
復制內(nèi)容到剪貼板
代碼:
<manifest
xmlns:android=" <application
android:icon="@drawable/app_notes"
android:label="@string/app_name">
<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),或者讓用戶選擇一個便箋并返回給調(diào)用者(action為 android.app.action.PICK)。
3、第三個是,當type為vnd.android.cursor.item/vnd.google.note時,返回給調(diào)用者一個用戶選擇的便箋(action為android.app.action.GET_CONTENT),而用戶卻不需要知道便箋從哪里讀取的。有了這些功能,下面的 Intent就會被解析到NotesList這個activity:
復制內(nèi)容到剪貼板
代碼:
{ 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返回給調(diào)用者。
{ action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note }:和上面的action為pick的Intent類似,不同的是這個Intent允許調(diào)用者(在這里指要調(diào)用NotesList的某個 Activity)指定它們需要返回的數(shù)據(jù)類型,系統(tǒng)會根據(jù)這個數(shù)據(jù)類型查找合適的 Activity(在這里系統(tǒng)會找到NotesList這個Activity),供用戶選擇便箋。
第二個Activity是com.google.android.notepad.NoteEditor,它為用戶顯示一條便箋,并且允許 用戶修改這個便箋。
它定義了兩個intent-filter,所以具有兩個功能。
***個功能是,當數(shù)據(jù)類型為 vnd.android.cursor.item/vnd.google.note時,允許用戶查看和修改一個便簽(action為 android.app.action.VIEW和android.app.action.EDIT)。
第二個功能是,當數(shù)據(jù)類型為 vnd.android.cursor.dir/vnd.google.note,為調(diào)用者顯示一個新建便箋的界面,并將新建的便箋插入到便箋列表中(action為android.app.action.INSERT)。
有了這兩個功能,下面的Intent就會被解析到NoteEditor這個activity:
復制內(nèi)容到剪貼板
代碼:
{ 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”這個便箋列表中創(chuàng)建一個新的空便箋,并允許用戶編輯這個便簽。當用戶保存這個便箋后,這個新便箋的URI將會返回給調(diào)用者。
***一個Activity是com.google.android.notepad.TitleEditor,它允許用戶編輯便箋的標題。
它可以被實現(xiàn)為一個應用可以直接調(diào)用(在Intent中明確設置component屬性)的類,不過這里我們將為你提供一個在現(xiàn)有的數(shù)據(jù)上發(fā)布可選操作的方法。
在這個 Activity的唯一的intent-filter中,擁有一個私有的action: com.google.android.notepad.action.EDIT_TITLE,表明允許用戶編輯便箋的標題。
和前面的view和edit 動作一樣,調(diào)用這個Intent 的時候,也必須指定具體的便箋(type為vnd.android.cursor.item/vnd.google.note)。不同的是,這里顯示和編輯的只是便箋數(shù)據(jù)中的標題。
除了支持缺省類別(android.intent.category.DEFAULT),標題編輯器還支持另外兩個標準類別: android.intent.category.ALTERNATIVE和
android.intent.category.SELECTED_ALTERNATIVE。
實現(xiàn)了這兩個類別之后,其它 Activity就可以調(diào)用queryIntentActivityOptions(ComponentName, Intent[], Intent, int)查詢這個Activity提供的action,而不需要了解它的具體實現(xiàn);
或者調(diào)用addIntentOptions(int, int, ComponentName, Intent[], Intent, int, Menu.Item[])建立動態(tài)菜單。需要說明的是,在這個intent-filter中有一個明確的名稱(通過android:label= "@string/resolve_title"指定),在用戶瀏覽數(shù)據(jù)的時候,如果這個Activity是數(shù)據(jù)的一個可選操作,指定明確的名稱可以為用戶提供一個更好控制界面。
有了這個功能,下面的Intent就會被解析到TitleEditor這個Activity:
復制內(nèi)容到剪貼板
代碼:
{ action=com.google.android.notepad.action.EDIT_TITLE data=content://com.google.provider.NotePad/notes/{ID}}:顯示并且允許用戶編輯標識為ID的便箋的標題。
【編輯推薦】