日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Android Intent和Intent Filter詳解(四)

 wanwanstudy 2012-02-11
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代碼:
  1. <manifest xmlns:android="http://schemas./apk/res/android"
  2. package="eoe.demo">
  3. <application android:icon="@drawable/app_notes"
  4. android:label="@string/app_name" >

  5. <provider android:name="NotePadProvider"
  6. android:authorities="com.google.provider.NotePad" />

  7. <activity android:name="NotesList" android:label="@string/title_notes_list">
  8. <intent-filter>
  9. <action android:name="android.intent.action.MAIN" />
  10. <category android:name="android.intent.category.LAUNCHER" />
  11. </intent-filter>
  12. <intent-filter>
  13. <action android:name="android.intent.action.VIEW" />
  14. <action android:name="android.intent.action.EDIT" />
  15. <action android:name="android.intent.action.PICK" />
  16. <category android:name="android.intent.category.DEFAULT" />
  17. <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
  18. </intent-filter>
  19. <intent-filter>
  20. <action android:name="android.intent.action.GET_CONTENT" />
  21. <category android:name="android.intent.category.DEFAULT" />
  22. <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
  23. </intent-filter>
  24. </activity>

  25. <activity android:name="NoteEditor"
  26. android:theme="@android:style/Theme.Light"
  27. android:label="@string/title_note" >
  28. <intent-filter android:label="@string/resolve_edit">
  29. <action android:name="android.intent.action.VIEW" />
  30. <action android:name="android.intent.action.EDIT" />
  31. <action android:name="com.android.notepad.action.EDIT_NOTE" />
  32. <category android:name="android.intent.category.DEFAULT" />
  33. <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
  34. </intent-filter>
  35. <intent-filter>
  36. <action android:name="android.intent.action.INSERT" />
  37. <category android:name="android.intent.category.DEFAULT" />
  38. <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
  39. </intent-filter>
  40. </activity>

  41. <activity android:name="TitleEditor"
  42. android:label="@string/title_edit_title"
  43. android:theme="@android:style/Theme.Dialog">
  44. <intent-filter android:label="@string/resolve_title">
  45. <action android:name="com.android.notepad.action.EDIT_TITLE" />
  46. <category android:name="android.intent.category.DEFAULT" />
  47. <category android:name="android.intent.category.ALTERNATIVE" />
  48. <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
  49. <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
  50. </intent-filter>
  51. </activity>
  52. </application>
  53. </manifest>
復(fù)制代碼

       第一個activity, NoteList, 和其它activity不同, 因?yàn)樗僮饕粋€筆記的目錄(筆記列表), 而不是一個單獨(dú)的筆記. 它一般作為該程序的初始界面. 它可以做以下三件事:

java代碼:
  1. <intent-filter>
  2. <action android:name="android.intent.action.MAIN" />
  3. <category android:name="android.intent.category.LAUNCHER" />
  4. </intent-filter>
復(fù)制代碼

        該filter聲明了記事本應(yīng)用程序的主入口. 標(biāo)準(zhǔn)的MAIN action是一個不需要任何其它信息(例如數(shù)據(jù)等)的程序入口, LAUNCHER category表示該入口應(yīng)該在應(yīng)用程序啟動器中列出.

java代碼:
  1. <intent-filter>
  2. <action android:name="android.intent.action.VIEW" />
  3. <action android:name="android.intent.action.EDIT" />
  4. <action android:name="android.intent.action.PICK" />
  5. <category android:name="android.intent.category.DEFAULT" />
  6. <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
  7. </intent-filter>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多