intent-filter的用法
首先贴一下google官方对intent-filter的解释
http://developer.android.com/guide/components/intents-filters.html
一个intent-filter至少包含3个要素
1)ACTION
2)CATEGORY
3)DATA
ACTION表示这个intent的动作,比如打电话,打开一个连接,打开一个图片,发短信,等等。也可以是一项服务
ACTION_CALL |
activity | Initiate a phone call. |
ACTION_EDIT |
activity | Display data for the user to edit. |
ACTION_MAIN |
activity | Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC |
activity | Synchronize data on a server with data on the mobile device. |
ACTION_BATTERY_LOW |
broadcast receiver | A warning that the battery is low. |
ACTION_HEADSET_PLUG |
broadcast receiver | A headset has been plugged into the device, or unplugged from it. |
ACTION_SCREEN_ON |
broadcast receiver | The screen has been turned on. |
ACTION_TIMEZONE_CHANGED |
broadcast receiver |
The setting for the time zone has changed. |
这是原图,可以看出一个intent中可以包含这个INTENT的一些信息,而ACTION_XXX就是其中之一,根据这个信息系统可以去调用对应的三大组件(activity,service,broadcase).
In each case, the Android system finds the appropriate activity, service, or set of broadcast receivers to respond to the intent, instantiating them if necessary.
这句话是上面网站中的,意思是根据intent系统可以找到对应的三大组件去启动,但是系统又是怎么去寻找的?
那就要用到intent-filter了
一个INTENT中一般包含以下几个组件
Component nameThe name of the component that should handle the intent.
我们一般在程序中就是这么用的,直接指定某一个Activity去handle这个intent,如果没有这一项,就要用到后面介绍的方法
The component name is optional. If it is set, the Intent object is delivered to an instance of the designated class. If it is not set, Android uses other information in the Intent object to locate a suitable target。摘自原文,意思同上。
ActionA string naming the action to be performed — or, in the case of broadcast intents, the action that took place and is being reported
Constant | Target component | Action |
---|---|---|
ACTION_CALL |
activity | Initiate a phone call. |
ACTION_EDIT |
activity | Display data for the user to edit. |
ACTION_MAIN |
activity | Start up as the initial activity of a task, with no data input and no returned output. |
ACTION_SYNC |
activity | Synchronize data on a server with data on the mobile device. |
ACTION_BATTERY_LOW |
broadcast receiver | A warning that the battery is low. |
ACTION_HEADSET_PLUG |
broadcast receiver | A headset has been plugged into the device, or unplugged from it. |
ACTION_SCREEN_ON |
broadcast receiver | The screen has been turned on. |
ACTION_TIMEZONE_CHANGED |
broadcast receiver | The setting for the time zone has changed. |
ACTION,表示了需要执行的动作,或者是为广播传递了一个消息
比如前4个,表示这个INTENT希望执行的动作,后4个就是为广播传递的消息了,如果有广播收到会有相应的举动。
See the Intent
class description for a list of pre-defined constants for generic actions. Other actions are defined elsewhere in the Android API. You can also define your own action strings for activating the components in your application.
点击可查看其它ACTION的内容,也可以自己定义ACTION的string,不是本文重点。
DataThe URI of the data to be acted on and the MIME type of that data. Different actions are paired with different kinds of data specifications. For example, if the action field is ACTION_EDIT
, the data field would contain the URI of the document to be displayed for editing. If the action is ACTION_CALL
, the data field would be a tel:
URI with the number to call. Similarly, if the action is ACTION_VIEW
and the data field is anhttp:
URI, the receiving activity would be called upon to download and display whatever data the URI refers to.
根据不同的ACTION,data会有不同的内容。上面说的很清楚了,就是根据你action paired with 的数据
When matching an intent to a component that is capable of handling the data, it's often important to know the type of data (its MIME type) in addition to its URI. For example, a component able to display image data should not be called upon to play an audio file.
当程序需要处理数据时候,最好比对一下data type和自己程序能处理的type,免得用播放器去打开一张图片
还有一个Category,暂时不用管
然后就是INTENT RESOLUTION
Intents can be divided into two groups:
- Explicit intents designate the target component by its name (the component name field, mentioned earlier, has a value set). Since component names would generally not be known to developers of other applications, explicit intents are typically used for application-internal messages — such as an activity starting a subordinate service or launching a sister activity.
-
Implicit intents do not name a target (the field for the component name is blank). Implicit intents are often used to activate components in other applications.
A different strategy is needed for implicit intents. In the absence of a designated target, the Android system must find the best component (or components) to handle the intent — a single activity or service to perform the requested action or the set of broadcast receivers to respond to the broadcast announcement
说明一个intent有显示(explicit)的和隐示(implicit)的,显示的会直接调用intent中的目标类进行启动(知道这个类的名字和位置),一个隐式Intent要想被一个组件处理,必须通过IntentFilter中具有和Intent对应的用于过滤Action,Data和Category的字段,然后才能启动
过滤ACTION,DATA,CATEGORY字段,就是字面上的意思
对于DATA,google上有更详细的介绍
http://developer.android.com/guide/topics/manifest/data-element.html
其中,
android:mimeType
A MIME media type, such as image/jpeg
or audio/mpeg4-generic
. The subtype can be the asterisk wildcard (*
) to indicate that any subtype matches.
It's common for an intent filter to declare a <data>
that includes only the android:mimeType
attribute.
Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC MIME types. As a result, you should always specify MIME types using lowercase letters.
scheme相当于协议,mimetype相当于打开文件的格式
android:scheme
The scheme part of a URI. This is the minimal essential attribute for specifying a URI; at least onescheme
attribute must be set for the filter, or none of the other URI attributes are meaningful.
A scheme is specified without the trailing colon (for example, http
, rather than http:
).
If the filter has a data type set (the mimeType
attribute) but no scheme, the content:
and file:
schemes are assumed.
Note: scheme matching in the Android framework is case-sensitive, unlike the RFC. As a result, you should always specify schemes using lowercase letters.
好的,基本上intent-filter的用法到这里就基本结束,对照例子看很容易就懂了