您的位置:首页 > 其它

Intent-filter的使用

2012-08-10 14:09 260 查看
如果一个 Intent 请求在一片数据上执行一个动作, Android 如何知道哪个应用程序(和组件)能用来响应这个请求呢? Intent Filter就是 用来注册 Activity 、 Service 和 Broadcast Receiver 具有能在某种数据上执行一个动作的能力。

使用 Intent Filter ,应用程序组件告诉 Android ,它们能为其它程序的组件的动作请求提供服务,包括同一个程序的组件、本地的或第三方的应用程序。

为了注册一个应用程序组件为 Intent 处理者,在组件的 manifest 节点添加一个 intent-filter 标签。

在 Intent Filter 节点里使用下面的标签(关联属性),你能指定组件支持的动作、种类和数据:
1.动作测试
< intent-filter >
< action android:name="com.example.project.SHOW_CURRENT" />
< action android:name="com.example.project.SHOW_RECENT" />
< action android:name="com.example.project.SHOW_PENDING" />
</ 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>子元素,比如:

< intent-filter . . . >
< category android:name="android.Intent.Category.DEFAULT" />
< category android:name="android.Intent.Category.BROWSABLE" />
</ intent-filter >


更多参考:http://www.vogella.com/articles/AndroidIntent/article.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: