Android 中的Intents and Intent Filters

使用步骤


创建Intent对象--------修饰Intent对象(设置action、data、category、extra)--------发送或启动Intent对象--------


---------Intent-filter与intent对象匹配-----------能够匹配该Intent的组件respond

android的中包含四大组建activity、services、content provider、broadcast  receivers

其中activities、services、broadcast  receivers这三个组件的启动与执行是通过Intent来完成的

创建Intent对象
          Intent intent = new Intent();

修饰Intent对象
    
 Intent对象设置action方法
 The action in an Intent object is set by the  setAction() method and read by  getAction().

Intent对象设置data和type
The  setData() method specifies data only as a URI,  setType() specifies it only as a MIME type, and  setDataAndType() specifies it as both a URI and a MIME type. The URI is read by  getData() and the type by  getType().

Intent对象设置category
The  addCategory() method places a category in an Intent object,  removeCategory() deletes a category previously added, and  getCategories() gets the set of all categories currently in the object. 

Intent设置附加数据
 the extras can be installed and read as a Bundle using the  putExtras() and  getExtras() methods. 


发送或启动intent的方法

sendStickyBroadcast()


发送后,相应的组件会对该Inbtent发生响应。

ntent-filter与intent对象匹配

activities、services、broadcast  receivers这三个组件在main都有一个Intent-filter标签,该标签中的子元素内容决定了该组件是否应该对启动的Intent发生反应

Intent filter标签的子元素列表有

<action>
<data>
<category>

列表中的标签可以可无,存在的话就需要进行匹配,如果有则只有在Intent对象相一致时才能匹配

<action>匹配规则
如果无action标签,那么所有的Intent对象将不能匹配,如果含有多个action,那么只要有一个能匹配Intent对象的action就可以通过匹配

category匹配规则

Intent对象中的category在Intent Filter中都必须存在,而且对于任何一个Intent对象都会包含一个默认的“android.intent.category.DEFAULT”,因此一个组件如果想要接受隐形的Intent,那么他的Intent filter必须包含“android.intent.category.DEFAULT”,否则将不能接收隐形Intent

data匹配规则

 When the URI in an Intent object is compared to a URI specification in a filter, it's compared only to the parts of the URI actually mentioned in the filter

 The data test compares both the URI and the data type in the Intent object to a URI and data type specified in the filter. The rules are as follows:
  1. An Intent object that contains neither a URI nor a data type passes the test only if the filter likewise does not specify any URIs or data types.
  2. An Intent object that contains a URI but no data type (and a type cannot be inferred from the URI) passes the test only if its URI matches a URI in the filter and the filter likewise does not specify a type. This will be the case only for URIs like mailto: and tel: that do not refer to actual data.

  3. An Intent object that contains a data type but not a URI passes the test only if the filter lists the same data type and similarly does not specify a URI.

  4. An Intent object that contains both a URI and a data type (or a data type can be inferred from the URI) passes the data type part of the test only if its type matches a type listed in the filter. It passes the URI part of the test either if its URI matches a URI in the filter or if it has a content: or file: URI and the filter does not specify a URI. In other words, a component is presumed to support content: and file: data if its filter lists only a data type.


总而言之  Intent对象中有的Intent-filter中也必须有,只能比Intent对象多





Internt 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的组件respond

匹配成功后或者会启动一个activity或者启动一个服务等对接收的Intent发生响应
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值