2. < action android:name="com.example.project.SHOW_CURRENT" />
3. < action android:name="com.example.project.SHOW_RECENT" />
4. < action android:name="com.example.project.SHOW_PENDING" />
5. </ 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>子元素,比如:
view source print ?
1. < intent-filter . . . >
2. < category android:name="android.Intent.Category.DEFAULT" />
3. < category android:name="android.Intent.Category.BROWSABLE" />
4. </ intent-filter >
只有当Intent请求中所有的Category与组件中某一个IntentFilter的<category>完全匹配时,才会让该 Intent请
求通过测试,IntentFilter中多余的<category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅
仅只会匹配没有设置类别的Intent请求。
3.数据测试
数据在<intent-filter>中的描述如下:
view source print ?
1. < intent-filter . . . >
2. < data android:type="video/mpeg" android:scheme="http" . . . />
3. < data android:type="audio/mpeg" android:scheme="http" . . . />
4. </ 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 来允许。
❑ HOMEHOME Activity 是设备启动(登陆屏幕)时显示的第一个 Activity 。通过指定 Intent Filter 为
HOME 种类而不指定动作的话,你正在将其设为本地 home 画面的替代。
❑ LAUNCHER使用这个种类来让一个 Activity 作为应用程序的启动项。
❑ datadata 标签允许你指定组件能作用的数据的匹配;如果你的组件能处理多个的话,你可以包含多个条件。你可以使用下面属性的任意组合来指定
组件支持的数据:
❑ 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 )。
接下来的代码片段显示了如何配置 Activity 的 Intent Filter ,使其以在特定数据下的默认的或可替代的动作的身份来执行
SHOW_DAMAGE动作。(创建地震内容将在下一章节。)
Java代码 1. <activity android:name=".EarthquakeDamageViewer"
2.
3. android:label="View Damage">
4.
5. <intent-filter>
6.
7. <action
8.
9. android:name="com.paad.earthquake.intent.action.SHOW_DAMAGE">
10.
11. </action>
12.
13. <category android:name="android.intent.category.DEFAULT"/>
14.
15. <category
16.
17. android:name="android.intent.category.ALTERNATIVE_SELECTED"
18.
19. />
20.
21. <data android:mimeType="vnd.earthquake.cursor.item/*"/>
22.
23. </intent-filter>
24.
25. </activity>
<activity android:name=".EarthquakeDamageViewer"
android:label="View Damage">
<intent-filter>
<action
android:name="com.paad.earthquake.intent.action.SHOW_DAMAGE">
</action>
<category android:name="android.intent.category.DEFAULT"/>
<category
android:name="android.intent.category.ALTERNATIVE_SELECTED"
/>
<data android:mimeType="vnd.earthquake.cursor.item/*"/>
</intent-filter>
</activity>