Android 开发文档 程序基础——Intent filters

来源:互联网 发布:广西财经学院网络 编辑:程序博客网 时间:2024/05/19 16:21

Intent对象可以明确命名一个目标组件。如果这么做了,Android会找到组件并激活。但是如果目标并没有明确的命名,Android必须定位一个最好的组件并反馈给intent。这是通过与可能的目标的Intent filters进行对比做到的。一个组件的intent filter告诉android组件可以处理的intents的种类。

一个组件可以有许多intent filters,每一个都定义一个不同的功能。如果没有intent filters,那么就只能通过明确命名组件为目标的方法来激活。

由于broadcast receiver在代码中创建和注册,所以intent filter被直接作为InterFilter对象。其他的filters都在manifest中建立。

转自我的android博客

原文

Intent filters

An Intent object can explicitly name a target component. If it does, Android finds that component (based on the declarations in the manifest file) and activates it. But if a target is not explicitly named, Android must locate the best component to respond to the intent. It does so by comparing the Intent object to the intent filters of potential targets. A component’s intent filters inform Android of the kinds of intents the component is able to handle. Like other essential information about the component, they’re declared in the manifest file. Here’s an extension of the previous example that adds two intent filters to the activity:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest . . . >
<application . . . >
<activity android:name=”com.example.project.FreneticActivity”
android:icon=”@drawable/small_pic.png”
android:label=”@string/freneticLabel”
. . .  >
<intent-filter . . . >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<intent-filter . . . >
<action android:name=”com.example.project.BOUNCE” />
<data android:mimeType=”image/jpeg” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
. . .
</application>
</manifest>

The first filter in the example — the combination of the action “android.intent.action.MAIN” and the category “android.intent.category.LAUNCHER” — is a common one. It marks the activity as one that should be represented in the application launcher, the screen listing applications users can launch on the device. In other words, the activity is the entry point for the application, the initial one users would see when they choose the application in the launcher.

The second filter declares an action that the activity can perform on a particular type of data.

A component can have any number of intent filters, each one declaring a different set of capabilities. If it doesn’t have any filters, it can be activated only by intents that explicitly name the component as the target.

For a broadcast receiver that’s created and registered in code, the intent filter is instantiated directly as an IntentFilter object. All other filters are set up in the manifest.

For more on intent filters, see a separate document, Intents and Intent Filters.

原创粉丝点击