您的位置:首页 > 移动开发 > Android开发

Android 中的Intents and Intent Filters

2012-11-15 15:31 369 查看

使用步骤

创建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的方法

startActivity()
 
startActivityForResult()

startService() 
bindService()
 
sendBroadcast()
,
sendOrderedBroadcast()

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:
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.

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.

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.

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发生响应
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: