Intent
阅读:http://developer.android.com/guide/components/intents-filters.html
intent可以用在:
1、Context.startActivity() or Activity.startActivityForResult() ;
2、Context.startService() 、Context.bindService() 。
3、Intent objects passed to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), or Context.sendStickyBroadcast()) 。
其实intent就是一个包含了信息的bundle:
Component name:接受该intent的组件,这是可选的,确定了系统就自动去找组件,如果没有确定那么系统就根据其他的信息确定最好的接受组件
Action:你想要进行的操作;
Data:你要操作的对象的URI,一般与Action配对,但是很重要的一点就是,要设置它的MIME(使用SetType)
Category:要操作的组件的类型;
Extras:附带的一些参数数据,主要是键值对,有一些Action要求提供参数,而且添加参数的形式多种多样。
Flags:定义了组件该怎么显示;
Only three aspects of an Intent object are consulted when the object is tested against an intent filter:
action
data (both URI and data type)
categoryThe extras and flags play no part in resolving which component receives an intent.
只有ACtion、data、category起到了过滤作用。
<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>
一个组件可以有多个ACtion;要起到过滤作用,只要intent的action符合上面其中一个即可。
<intent-filter . . . > <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> . . . </intent-filter>
For an intent to pass the category test, every category in the Intent object must match a category in the filter. The filter can list additional categories, but it cannot omit any that are in the intent.
<intent-filter . . . > <data android:mimeType="video/mpeg" android:scheme="http" . . . /> <data android:mimeType="audio/mpeg" android:scheme="http" . . . /> . . . </intent-filter>
data的一般格式为:scheme://host:port/path,例如content://com.example.project:200/folder/subfolder/etc。
格式里的各部分都是可选的,但是必须按照顺序来,比如要有path,就得现有host等等。
系统的匹配也是从左往右的,先匹配scheme然后一个个来。
注意,属性里还能声明MIME type。
匹配规则:
- 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:andtel: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:orfile:URI and the filter does not specify a URI. In other words, a component is presumed to supportcontent:andfile:data if its filter lists only a data type.
<data android:mimeType="image/*" /> <data android:scheme="http" android:type="video/*" />
还有上面两种过滤条件,特别的是对于第二种,如果浏览器无法打开,那么就会调用ACtivity去接受这个DATA,如果Activity接受不了,则调用下载器下载。
Therefore, the
DEFAULTcategory is required for all filters — except for those with theMAINaction andLAUNCHERcategory. (Intent filters are not consulted for explicit intents.)
文章最后有一个例子,可以作为参考使用。

浙公网安备 33010602011771号