Using intent matching 使用intent匹配
intent和intent filter相匹配, 不僅為了尋找并啟動一個目標(biāo)組件, 也是為了尋找設(shè)備上組件的信息. 例如,
android系統(tǒng)啟動了應(yīng)用程序啟動器, 該程序位于屏幕的頂層, 顯示了用戶可以啟動的程序,
這是通過查找設(shè)備上所有的action為"android.intent.action.MAIN"
,category為"android.intent.category.LAUNCHER"的intent
filter所在的activity實(shí)現(xiàn)的. 然后它顯示了這些activity的圖標(biāo)和標(biāo)題. 類似的, 它通過尋找
"android.intent.category.HOME"的filter來定位主屏幕程序.
應(yīng)用程序可以用相同的方式來使用intent匹配. PackageManager
有一組query...()方法來尋找接受某個特定intent的所有組件,
還有一系列resolve...()方法來決定響應(yīng)一個intent的最佳組件. 例如,
queryIntentActivities()返回一個activity列表, 這些activity可以執(zhí)行傳入的intent.
類似的還有queryIntentServices()和queryIntentBroadcastReceivers().
Note Pad Example 例子:記事本
記事本示例程序讓用戶可以瀏覽一個筆記列表, 查看, 編輯, 刪除和增加筆記. 這一節(jié)關(guān)注該程序定義的intent filter.
在其manifest文件中, 記事本程序定義了三個activity, 每個有至少一個intent filter. 它還定義了一個content provider來管理筆記數(shù)據(jù). manifest 文件如下:
java代碼:
- <manifest xmlns:android="http://schemas./apk/res/android"
- package="eoe.demo">
- <application android:icon="@drawable/app_notes"
- android:label="@string/app_name" >
- <provider android:name="NotePadProvider"
- android:authorities="com.google.provider.NotePad" />
- <activity android:name="NotesList" android:label="@string/title_notes_list">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <action android:name="android.intent.action.EDIT" />
- <action android:name="android.intent.action.PICK" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.GET_CONTENT" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
- </intent-filter>
- </activity>
- <activity android:name="NoteEditor"
- android:theme="@android:style/Theme.Light"
- android:label="@string/title_note" >
- <intent-filter android:label="@string/resolve_edit">
- <action android:name="android.intent.action.VIEW" />
- <action android:name="android.intent.action.EDIT" />
- <action android:name="com.android.notepad.action.EDIT_NOTE" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.INSERT" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
- </intent-filter>
- </activity>
- <activity android:name="TitleEditor"
- android:label="@string/title_edit_title"
- android:theme="@android:style/Theme.Dialog">
- <intent-filter android:label="@string/resolve_title">
- <action android:name="com.android.notepad.action.EDIT_TITLE" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.ALTERNATIVE" />
- <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
- <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
復(fù)制代碼
第一個activity, NoteList, 和其它activity不同, 因?yàn)樗僮饕粋€筆記的目錄(筆記列表), 而不是一個單獨(dú)的筆記. 它一般作為該程序的初始界面. 它可以做以下三件事:
java代碼:
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
復(fù)制代碼
該filter聲明了記事本應(yīng)用程序的主入口. 標(biāo)準(zhǔn)的MAIN action是一個不需要任何其它信息(例如數(shù)據(jù)等)的程序入口, LAUNCHER category表示該入口應(yīng)該在應(yīng)用程序啟動器中列出.
java代碼:
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <action android:name="android.intent.action.EDIT" />
- <action android:name="android.intent.action.PICK" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
- </intent-filter>
|