`
weiweirenhong
  • 浏览: 32580 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
社区版块
存档分类
最新评论

Intents and Intent Filters--Intent filters

阅读更多
Intent filters

   To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters. Each filter describes a capability of the component, a set of intents that the component is willing to receive. It, in effect, filters in intents of a desired type, while filtering out unwanted intents — but only unwanted implicit intents (those that don't name a target class). An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters.

翻译:三类核心组件为了使得系统知晓哪些隐式intent对象可以被自己所响应,通常声明一个或多个intent过滤器,过滤器的作用是声明当前组件的能力,表示当前组件所能接受的intent对象。需要注意的是如果是显式intent,intent过滤器是不起任何作用的,这时候显式intent直接发送给目标组件实例(当然你可以使用extras和flags来传递必要的数据和发送相关的系统指令)。而对于隐式intent,只有当它通过某个 intent过滤器三个测试项的检测后才可以被发送到某个组件实例。

A component has separate filters for each job it can do, each face it can present to the user. For example, the NoteEditor activity of the sample Note Pad application has two filters — one for starting up with a specific note that the user can view or edit, and another for starting with a new, blank note that the user can fill in and save. (All of Note Pad's filters are described in the Note Pad Example section, later.)

翻译:一个组件可以执行不同类型的操作,在manifest文件中的组件元素的定义中,针对不同的操作可以分别多个intent过滤器子元素,例如在该系列文档中提供的Note Pad样例应用中的NoteEditor activity,就定义了两个过滤器,一个过滤器针对用户查阅或编辑Note的操作,另外一个过滤器针对用户新建Note的操作,换句话说表示该 activity可以执行两种不同类型的操作。

Filters and security   An intent filter cannot be relied on for security. While it opens a component to receiving only certain kinds of implicit intents, it does nothing to prevent explicit intents from targeting the component. Even though a filter restricts the intents a component will be asked to handle to certain actions and data sources, someone could always put together an explicit intent with a different action and data source, and name the component as the target.【翻译:intent过滤器不能用于安全控制管理,intent过滤器的作用是为某个隐式intent打开一个组件,它无法阻止显式intent直接启动目标组件。】


  An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as <intent-filter> elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)

翻译: intent filter是IntentFilter类的实例,然而,由于Android系统在它启动某个组件实例之前,就必须知道这个组件能做什么操作,所以intent过滤器的创建是不会使用Java代码来创建的的,而是在应用的manifest文件中直接定义intentFilter元素的。(这里有一个例外:直接通过Context.registerReceiver()方法动态注册的广播接收组件的过滤器是直接创建过滤器实例的。)

A filter has fields that parallel the action, data, and category fields of an Intent object. An implicit intent is tested against the filter in all three areas. To be delivered to the component that owns the filter, it must pass all three tests. If it fails even one of them, the Android system won't deliver it to the component — at least not on the basis of that filter. However, since a component can have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another.

翻译:一个过滤器中包含与隐式intent对等的三类信息(action, data, category),一个隐式intent要能够激活某个组件的前提是:必须通过某个过滤器这三项(action, data, category)比对测试,

Each of the three tests is described in detail below:

一、Action test
    An <intent-filter> element in the manifest file lists actions as <action> subelements. For example【翻译:manifest文件中的intent过滤器元素中可以如下定义action子元素:】:

   
引用
<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>


    As the example shows, while an Intent object names just a single action, a filter may list more than one. The list cannot be empty; a filter must contain at least one <action> element, or it will block all intents.【翻译:上例中的过滤器至少应该有一个action,否则该过滤器将阻塞所有隐式intent。】

    To pass this test, the action specified in the Intent object must match one of the actions listed in the filter. If the object or the filter does not specify an action, the results are as follows: 【翻译:一个隐式intent要想通过某个intent过滤器的action测试,该intent实例的action必须能够和当前intent过滤器中的某个action匹配。如果intent实例或是intent过滤器中没有指定action的话,将会发生以下测试结果】:

        1、If the filter fails to list any actions, there is nothing for an intent to match, so all intents fail the test. No intents can get through the filter.【翻译:如果是intent过滤器中没有指定action,那么匹配肯定失败,导致该过滤器将阻塞所有隐式intent。】

        2、On the other hand, an Intent object that doesn't specify an action automatically passes the test — as long as the filter contains at least one action.【翻译:如果是intent实例中没有指定action的话,只要该过滤器声明了至少一个action的话,该intent实例将通过当前action测试。】


二、Category test
    An <intent-filter> element also lists categories as subelements. For example:【翻译:manifest文件中的intent过滤器元素中可以如下定义category子元素:】

  
引用
<intent-filter . . . >
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        . . .
    </intent-filter>


    Note that the constants described earlier for actions and categories are not used in the manifest file. The full string values are used instead. For instance, the "android.intent.category.BROWSABLE" string in the example above corresponds to the CATEGORY_BROWSABLE constant mentioned earlier in this document. Similarly, the string "android.intent.action.EDIT" corresponds to the ACTION_EDIT constant.【翻译:注意:在manifest文件中,过滤器元素中定义的action、category子元素不使用Android API中的常量名,而是直接使用常量值。】

    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实例要想通过类别测试的话,在当前这个intent实例中的每个类别信息都必须和当前的intent过滤器中的类别列表中的值匹配上,也就是说当前intent过滤器中可以有多于该intent实例中的类别信息个数。

    In principle, therefore, an Intent object with no categories should always pass this test, regardless of what's in the filter. That's mostly true. However, with one exception, Android treats all implicit intents passed to startActivity() as if they contained at least one category: "android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters. (Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. They mark activities that begin new tasks and that are represented on the launcher screen. They can include "android.intent.category.DEFAULT" in the list of categories, but don't need to.) See Using intent matching, later, for more on these filters.)

    翻译:原则上说,如果当前这个intent实例中没有类别信息的话,它总是可以通过类别测试的,绝大多数场合是这样的,但是也有个例外:由于 Android系统对于通过StartActivity()方法传递的隐式intent都会自动添加一个 android.intent.category.DEFAULT 类别属性信息,所以对于intent过滤器来说常常必须定义一个android.intent.category.DEFAULT 类别属性信息。对于那些定义了android.intent.action.MAIN 和 android.intent.category.LAUNCHER 类别的intent过滤器来说不是因为这个原因而在intent过滤器中定义了android.intent.category.LAUNCHER类别属性, 标记有这两个类别信息的intent过滤器有特殊的含义:要求当前intent过滤器所关联的组件(Activity)在设备发射屏以新task的方式被立即激活(按下menu键后),而且这时候包含android.intent.category.LAUNCHER 类别不是必须的。

三、Data test
    Like the action and categories, the data specification for an intent filter is contained in a subelement. And, as in those cases, the subelement can appear multiple times, or not at all. For example:

   
引用
<intent-filter . . . >
        <data android:mimeType="video/mpeg" android:scheme="http" . . . />
        <data android:mimeType="audio/mpeg" android:scheme="http" . . . />
        . . .
    </intent-filter>


    Each <data> element can specify a URI and a data type (MIME media type). There are separate attributes — scheme, host, port, and path — for each part of the URI:

    翻译:每个data元素由URI和data类型组成,其中URI是由:schema、host、Port、path四部分组成的。

    scheme://host:port/path

    For example, in the following URI,

    content://com.example.project:200/folder/subfolder/etc

    the scheme is "content", the host is "com.example.project", the port is "200", and the path is "folder/subfolder/etc". The host and port together constitute the URI authority; if a host is not specified, the port is ignored.

    翻译:上例中的scheme="content", host ="com.example.project", port ="200", path= "folder/subfolder/etc", URI中的host和Port组成了URI的authority,如果host未指定的话那么Port即使指定了也被系统忽略。

    Each of these attributes is optional, but they are not independent of each other: For an authority to be meaningful, a scheme must also be specified. For a path to be meaningful, both a scheme and an authority must be specified.

    翻译:URI中的各个组成部分是可选的,但是又是关联的,如果要使得URI的authority有意义,必须先指定scheme,要使得path有意义,又必须先指定URI的authority

    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. For example, if a filter specifies only a scheme, all URIs with that scheme match the filter. If a filter specifies a scheme and an authority but no path, all URIs with the same scheme and authority match, regardless of their paths. If a filter specifies a scheme, an authority, and a path, only URIs with the same scheme, authority, and path match. However, a path specification in the filter can contain wildcards to require only a partial match of the path.

    翻译:data测试中的URI测试通过的原则是:只要当前intent过滤器中列举出的URI部分与当前intent中的URI信息可以匹配即通过Uri测试。需要注意的是在intent过滤器的URI中的path定义中可以使用通配符

    The type attribute of a <data> element specifies the MIME type of the data. It's more common in filters than a URI. Both the Intent object and the filter can use a "*" wildcard for the subtype field — for example, "text/*" or "audio/*" — indicating any subtype matches.

    翻译:注意 在intent过滤器的data 类型定义中可以使用通配符

    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.

    翻译:data 测试基本原则如下:

       1. 如果一个Intent对象中既不包含URI也不包含data type,这时候如果当前intent过滤器同样也没有指定URI和data type的时候,data测试通过。

       2. 如果一个Intent对象中包含URI但是不包含date type(而且数据类型无法从URI中推断出),这时候如果当前intent过滤器同样只是指定了相应的URI的场合,data测试通过。以上场合仅仅适用于 URIs 是 mailto: 和 tel: 无法推断实际数据类型的时候。

       3. 如果一个Intent对象中包含date type但是不包含URI,这时候如果当前intent过滤器同样只是例举了相应的date type的场合,data测试通过。

       4. 如果一个Intent对象中同时包含URI和date type(或数据类型可以从URI中推断出),这时候如果当前intent过滤器列举了相应的date type的场合,data type部分的测试通过。如果当前intent过滤器也列举了相应的URI,则通过URI部分的测试。这时候如果说当前intent指定的data URI是content:或 file:的时候,即使当前过滤器没有指定URI,也通过URI部分的测试。换句话说,当组件过滤器中仅仅指定了data类型而没有明确指定URI的时候,组件是默认支持URI==content:或 file:的。

If an intent can pass through the filters of more than one activity or service, the user may be asked which component to activate. An exception is raised if no target can be found. 【翻译:如果一个intent通过一个以上的activity或service的过滤器测试的话,这时候系统将让用户选择哪个组件来响应该intent请求,如果没有找到目标组件来响应当前intent的话,将发生异常。】

Common cases

The last rule shown above for the data test, rule (d), reflects the expectation that components are able to get local data from a file or content provider. Therefore, their filters can list just a data type and do not need to explicitly name the content: and file: schemes. This is a typical case. A <data> element like the following, for example, tells Android that the component can get image data from a content provider and display it【翻译:上面说到的第四个原则使得组件能够从文件或内容提供器中读取本地数据,所以,作为过滤器可以仅仅例举数据类型、不需要明确指定content: 和file:schemes,这是很典型的,像下面的<data>元素中就是这样做的,它告知Android系统该组件可以从内容提供器中获得图像数据,然后显示相关的图像数据。】:

引用
<data android:mimeType="image/*" />


Since most available data is dispensed by content providers, filters that specify a data type but not a URI are perhaps the most common.【翻译:由于绝大多数数据都是由内容提供器提供的,那些指定了数据类型而没有指定URI的过滤器是这样的典型。】

Another common configuration is filters with a scheme and a data type. For example, a <data> element like the following tells Android that the component can get video data from the network and display it:【翻译:另外一个很常见的配置过滤器的方式是使用scheme 和数据类型来配置过滤器,例如下面的例子,告知Android系统该组件可以通过网络取得视频数据并显示该视频数据。】
引用

<data android:scheme="http" android:type="video/*" />


Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.【翻译:想一下:当用户根据一个链接要显现相关的画面数据,这时候浏览器应用程序首先做的是显示数据,如果无法显示的话,将会创建一个隐式intent,其中包含了数据类型和schema等信息,然后请求一个activity来做数据显示工作,显示数据需要下载管理器先下载数据,然后把下载的数据交给某个内容提供器控制。。。】

Most applications also have a way to start fresh, without a reference to any particular data. Activities that can initiate applications have filters with "android.intent.action.MAIN" specified as the action. If they are to be represented in the application launcher, they also specify the "android.intent.category.LAUNCHER" category:【翻译:多数应用也有另外一种途径来启动更新(无需对任何数据的引用),activity实例可以设定它自己的action属性="android.intent.action.MAIN",如果这个实例需要在发射台上呈现出来的话,可以设定它的category属性="android.intent.category.LAUNCHER"】

引用
<intent-filter . . . >
    <action android:name="code android.intent.action.MAIN" />
    <category android:name="code android.intent.category.LAUNCHER" />
</intent-filter>
分享到:
评论

相关推荐

    Intents and Intent Filters 理论中英双文

    内含四个pdf文件,分别为 Intent and Intent-filter Intents and Intent Filters理论英文 Intents and Intent Filters理论中文 Intent入门指南 详尽介绍关于android intent

    Android Intents and Intent Filters(一)

    Android Intents and Intent Filters(一) 对应博客地址:http://blog.csdn.net/michael__li/article/details/6947545

    Android Intents and Intent Filters(二)源代码

    Android Intents and Intent Filters(二)源代码 对应博客 http://blog.csdn.net/michael__li/article/details/6950127

    Android系列教程之十二:Intents and Intent Filters(三).docx

    Android系列教程之十二:Intents and Intent Filters(三)

    Android开发之旅 Intents和Intent Filters(实例部分)(免费)

    Android开发之旅 Intents和Intent Filters(实例部分)(免费)

    Android开发之旅 Intents和Intent Filters(理论部分)(免费)

    Android开发之旅 Intents和Intent Filters(理论部分)(免费)

    Android Intent和Intent Filter详解

    Intents and Intent Filters  三种应用程序基本组件——activity, service和broadcast receiver——是使用称为intent的消息来激活的。Intent消息传递是一种组件间运行时绑定的机制. intent是Intent对象, 它包含了...

    Android意图和意图过滤器

    1 意图和意图过滤器Intents and Intent Filters 2 意图对象Intent Objects 3 意图解析Intent Resolution 4 过滤器与安全Filters and security

    Android开发之旅

    Android开发之旅:环境搭建及HelloWorld 1 Android开发之旅:HelloWorld项目的目录结构 2 Android开发之旅:android架构 3 Android开发之旅:应用程序...Android开发之旅 Intents和Intent Filters(实例部分) 17

    Android开发指南中文版-----应用程序框架

    意图和意图过滤器Intents and Intent Filters 43 意图过滤器Intent filters 47 通常情况Common cases 51 使用意图匹配Using intent matching 52 数据存储Data Storage 52 概览Storage quickview 52 ? 系统偏好:快速...

    Android开发入门与实战+和第二版部分章节+第二版源代码

    本书主要内容为:Android开发环境搭建、Android SDK介绍、Android应用程序结构剖析,并对Android 中最重要的组件Activity、Intents&Intent Filters&Broadcast receivers、Intent、Service、Content Providers进行了...

    Android开发宝典.rar

    意图和意图过滤器Intents and Intent Filters 43 意图过滤器Intent filters 47 通常情况Common cases 51 使用意图匹配Using intent matching 52 数据存储Data Storage 52 概览Storage quickview 52  系统...

    Android开发指南中文版

    意图和意图过滤器Intents and Intent Filters 43 意图过滤器Intent filters 47 通常情况Common cases 51 使用意图匹配Using intent matching 52 数据存储Data Storage 52 概览Storage quickview 52  系统偏好:...

    Apress.Android.Recipes.2nd.Edition.2012

    Understanding Intents and Intent Filters Chapter 12. Advanced Android Topics BOOK DETAILS Paperback: 960 pages Publisher: Apress; 2nd Edition (December 2012) Language: English ISBN-10: 1430246146 ...

    Apress.Android.Apps.for.Absolute.Beginners.2nd.edition

    Anybody can start building simple apps for the Android platform, and this book will show you how! Recently updated to include Android ...Understanding Intents and Intent Filters Advanced Android Topics

    Android开发之旅——完整版

    挺不错的开发资料,上传赚分。 • Android 开发之旅:环境搭建及HelloWorld • Android 开发之旅:HelloWorld项目的目录结构 • Android 开发之旅:...• Android开发之旅: Intents和Intent Filters(理论部分)

    Android开发指南

    意图和意图过滤器Intents and Intent Filters 一个应用程序的三个核心组件-活动,服务和广播接收器是通过消息即意图(Intents)来激活的。Intent消息传送是相同或不同应用中组件运行时晚绑定的一种机制。意图本身...

    Android开发之旅 完整版pdf

    作者:吴秦 ...本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). • Android 开发之旅:环境...• Android开发之旅: Intents和Intent Filters(理论部分)

Global site tag (gtag.js) - Google Analytics