`

【翻译】(10)意图与意图过滤器

 
阅读更多

【翻译】(10)意图与意图过滤器

 

see

http://developer.android.com/guide/topics/intents/intents-filters.html

 

原文见

http://developer.android.com/guide/topics/intents/intents-filters.html

 

-------------------------------

 

Intents and Intent Filters

 

意图与意图过滤器

 

In this document

 

本文目录

 

* Intent Objects 意图对象

* Intent Resolution 意图解析(注:划分)

* Intent filters 意图过滤器

* Common cases 一般情况

* Using intent matching 使用意图匹配

* Note Pad Example 记事本示例

 

Key classes

 

关键类

 

Intent

IntentFilter

BroadcastReceiver

PackageManager

 

-------------------------------

 

Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. Intent messaging is a facility for late run-time binding between components in the same or different applications. The intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed — or, often in the case of broadcasts, a description of something that has happened and is being announced. There are separate mechanisms for delivering intents to each type of component:

 

应用程序的三种核心组件——活动,服务,和广播接收器——通过被称为意图的消息所激活。意图消息是一种设施,用于相同或不同应用程序中的组件的延迟运行时绑定。意图自身,一个Intent对象,是一个被动数据结构,持有要执行操作的抽象描述——或,常常在广播的情况下,描述要执行操作的某些东西的描述。不同类型的组件有单独的传递意图的机制。

 

* An Intent object is passed to Context.startActivity() or Activity.startActivityForResult() to launch an activity or get an existing activity to do something new. (It can also be passed to Activity.setResult() to return information to the activity that called startActivityForResult().)

 

* 一个Intent对象被传递给Context.startActivity()或Activity.startActivityForResult()以启动一个活动或获取一个现存活动去做某些新的事情。(它还可以被传递给Activity.setResult()以返回信息给调用startActivityForResult()的活动。)

 

* An Intent object is passed to Context.startService() to initiate a service or deliver new instructions to an ongoing service. Similarly, an intent can be passed to Context.bindService() to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.

 

* 一个Intent对象被传递给Context.startService()以激活一个服务或传递新的指令到一个正在运行的服务。类似地,一个意图可以被传递给Context.bindService()以建立一个在调用方组件和目标服务之间的连接。它可以可选地初始化服务,如果它还没有运行。

 

* Intent objects passed to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), or Context.sendStickyBroadcast()) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code.

 

* 传递给任意广播方法(诸如Context.sendBroadcast(),Context.sendOrderedBroadcast(),或 Context.sendStickyBroadcast())的Intent对象被传递给所有感兴趣的广播接收器。许多类型的广播起源于系统代码。

 

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. There is no overlap within these messaging systems: Broadcast intents are delivered only to broadcast receivers, never to activities or services. An intent passed to startActivity() is delivered only to an activity, never to a service or broadcast receiver, and so on.

 

在每种情况下,Android系统查找合适的活动,服务,或广播接收器集合以响应意图,在需要时实例化它们。这些消息系统之间没有重叠:Broadcast(注:广播)意图只传递给广播接收器,从不传递给活动或服务。一个传递给startActivity()的意图只传递给活动,从不传递给一个服务或广播接收器,如此类推。

 

This document begins with a description of Intent objects. It then describes the rules Android uses to map intents to components — how it resolves which component should receive an intent message. For intents that don't explicitly name a target component, this process involves testing the Intent object against intent filters associated with potential targets.

 

本文开头是对Intent对象的描述。然后描述Android用来映射意图到组件的规则——它如何解析哪个组件应该接受意图消息。对于没有显式命名目标组件的意图,这个过程涉及测试Intent对象是否符合与隐式目标关联的意图过滤器。

 

-------------------------------

 

Intent Objects

 

意图对象

 

An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity). Principally, it can contain the following:

 

一个Intent对象是一束信息。它包含对接收意图的组件来说有用的信息(诸如采取的动作和操作的数据)以及对于Android系统来说有用的信息(诸如应该处理关于如何启动目标活动的意图和指令的组件分类)。主要地,它可以包含以下部分:

 

* Component name

 

* 组件名称

 

The name of the component that should handle the intent. This field is a ComponentName object — a combination of the fully qualified class name of the target component (for example "com.example.project.app.FreneticActivity") and the package name set in the manifest file of the application where the component resides (for example, "com.example.project"). The package part of the component name and the package name set in the manifest do not necessarily have to match.

 

应该处理意图的组件名称。这个域是ComponentName对象——一个目标组件的完全标准化类名的组合(例如"com.example.project.app.FreneticActivity")和设置在组件驻留的应用程序的清单文件中的包名(例如,"com.example.project")。设置在清单中的组件名和包名的包部分没必要一定匹配。

 

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 — see Intent Resolution, later in this document.

 

组件名是可选的。如果它被设置,Intent对象被传递到指定类的实例。如果它没有被设置,Android使用Intent对象中的其它信息定位合适的目标——见文档后面的意图解析。

 

The component name is set by setComponent(), setClass(), or setClassName() and read by getComponent().

 

组件名被setComponent(),setClass()或setClassName()设置,并被getComponent()读取。

 

* Action

 

* 动作

 

A string naming the action to be performed — or, in the case of broadcast intents, the action that took place and is being reported. The Intent class defines a number of action constants, including these:

 

一个命名要执行动作的字符串——或者,在广播意图的情况下,发生并被报告的动作。Intent类定义一些动作常量,包括这些:

 

-------------------------------

 

Constant Target component Action

 

常量 目标组件 动作

 

ACTION_CALL activity Initiate a phone call.

 

ACTION_CALL 活动 初始化电话呼叫

 

ACTION_EDIT activity Display data for the user to edit.

 

ACTION_EDIT 活动 向用户显示要编辑的数据

 

ACTION_MAIN activity Start up as the initial activity of a task, with no data input and no returned output.

 

ACTION_MAIN 活动 启动作为任务的初始活动,没有数据输入也没有返回的输出。

 

ACTION_SYNC activity Synchronize data on a server with data on the mobile device.

 

ACTION_SYNC 活动 同步服务器上的数据和移动设备上的数据。

 

ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low.

 

ACTION_BATTERY_LOW 广播接收器 电量低的警告

 

ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the device, or unplugged from it.

 

ACTION_HEADSET_PLUG 广播接收器 耳机已被插入设备,或从它拔出。

 

ACTION_SCREEN_ON broadcast receiver The screen has been turned on.

 

ACTION_SCREEN_ON 广播接收器 屏幕已打开

 

ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has changed.

 

ACTION_TIMEZONE_CHANGED 广播接收器 时区设置已改变。

 

-------------------------------

 

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. Those you invent should include the application package as a prefix — for example: "com.example.project.SHOW_COLOR".

 

参见Intent类描述以获取通常动作的预定义常量列表。其它动作被定义在Android API中的其它地方。你还可以定义你自己的动作字符串以激活你的应用程序中的组件。那些你发明的常量应该包含应用程序包名作为前缀——例如:"com.example.project.SHOW_COLOR"。

 

The action largely determines how the rest of the intent is structured — particularly the data and extras fields — much as a method name determines a set of arguments and a return value. For this reason, it's a good idea to use action names that are as specific as possible, and to couple them tightly to the other fields of the intent. In other words, instead of defining an action in isolation, define an entire protocol for the Intent objects your components can handle.

 

动作很大程度决定意图的其余部分是如何构成——特别是data和extras域——很像方法名决定(注:对应)一组参数和一个返回值。因为这个原因,最好尽可能使用指定的动作名,并且把它们紧密地耦合到意图的其它域上。换言之,不是单独地定义动作,而是定义你的组件可以处理的Intent对象的整个协议。

 

The action in an Intent object is set by the setAction() method and read by getAction().

 

一个Intent对象被setAction()方法设置并被getAction()读取。

 

* Data

 

* 数据

 

The 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 an http: URI, the receiving activity would be called upon to download and display whatever data the URI refers to.

 

要被作用在的数据URI和那些数据的MIME类型。不同的动作被配对到不同类型的数据规范。例如,如果action域是ACTION_EDIT,那么data域将包含要被显示以便编辑的文档的URI。如果action是ACTION_CALL,那么data域将是一个tel: URI,带有要呼叫的号码。类似地,如果action是ACTION_VIEW而data域是一个http: URI,那么接收的活动将被调用以下载和显示URI引用的任何数据。

 

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.

 

当匹配一个意图到能处理数据的组件时,通常重要的是去了解数据类型(它的MIME类型)以及它的URI。例如,一个可以显示图像数据的组件不应该被调用去播放一个音频文件。

 

In many cases, the data type can be inferred from the URI — particularly content: URIs, which indicate that the data is located on the device and controlled by a content provider (see the separate discussion on content providers). But the type can also be explicitly set in the Intent object. 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().

 

在很多情况下,数据类型可以从URI中推断——特别地content: URI,它指示位于设备上的数据并被内容提供者控制(参见关于内容提供者的单独讨论)。但类型还可以被显式地设置在Intent对象中。setData()方法指定数据仅作为URI,setType()指定它仅作为MIME类型,而setDataAndType()指定它作为URI以及MIME类型。URI被getData()读取,而类型被getType()读取。

 

* Category

 

* 分类

 

A string containing additional information about the kind of component that should handle the intent. Any number of category descriptions can be placed in an Intent object. As it does for actions, the Intent class defines several category constants, including these:

 

包含关于应该处理意图的组件类型的额外信息的字符串。任意数量的分类描述可以被放置进一个Intent对象。就像对动作所做的那样,Intent类定义几个分类常量,包括这些:

 

-------------------------------

 

Constant Meaning

 

常量 含义

 

CATEGORY_BROWSABLE The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.

 

CATEGORY_BROWSABLE 目标活动可以安全地被浏览器调用以显示被一个链接引用的数据——例如,一个图片或一个电子邮件消息。

 

CATEGORY_GADGET The activity can be embedded inside of another activity that hosts gadgets.

 

CATEGORY_GADGET 活动可以被嵌入到另一个拥有小部件的活动内。

 

CATEGORY_HOME The activity displays the home screen, the first screen the user sees when the device is turned on or when the Home button is pressed.

 

CATEGORY_HOME 活动显示桌面屏幕(注:主页屏),当设备被打开或主页按钮被按下时用户看到的第一个屏幕。

 

CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher.

 

CATEGORY_LAUNCHER 活动可以是任务的初始化活动并被列举在顶级应用程序启动器中。

 

CATEGORY_PREFERENCE The target activity is a preference panel.

 

CATEGORY_PREFERENCE 目标活动是一个预设面板。

 

-------------------------------

 

See the Intent class description for the full list of categories.

 

参见Intent的类描述以获取完整的分类列表。

 

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.

 

addCategory()方法放置一个分类进Intent对象,removeCategory()删除之前添加的分类,而getCategories()获取对象内最近的所有分类集合。

 

* Extras

 

* 额外数据

 

Key-value pairs for additional information that should be delivered to the component handling the intent. Just as some actions are paired with particular kinds of data URIs, some are paired with particular extras. For example, an ACTION_TIMEZONE_CHANGED intent has a "time-zone" extra that identifies the new time zone, and ACTION_HEADSET_PLUG has a "state" extra indicating whether the headset is now plugged in or unplugged, as well as a "name" extra for the type of headset. If you were to invent a SHOW_COLOR action, the color value would be set in an extra key-value pair.

 

应该被传递给处理意图的组件的附加信息的键值对。正如一些动作配对特定数据URI类型那样,一些动作也被配对特定的额外数据。例如,一个ACTION_TIMEZONE_CHANGED意图拥有time-zone额外数据来指定新的时区,而ACTION_HEADSET_PLUG拥有一个state额外数据来指示耳机现在是插入还是拔出,以及一个name额外数据表示耳机类型。如果你打算发明一个SHOW_COLOR对象,那么颜色值应该被设置在一个额外键值对中。

 

The Intent object has a series of put...() methods for inserting various types of extra data and a similar set of get...() methods for reading the data. These methods parallel those for Bundle objects. In fact, the extras can be installed and read as a Bundle using the putExtras() and getExtras() methods.

 

Intent对象有一系列put...()方法用于插入不同类型的额外数据,以及一个相似的get...()方法集合用于读取数据。这些方法对应Bundle对象的方法。事实上,额外数据可以被安装并作为Bundle对象使用putExtras()和getExtras()方法进行读取。

 

* Flags

 

* 标志

 

Flags of various sorts. Many instruct the Android system how to launch an activity (for example, which task the activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities). All these flags are defined in the Intent class.

 

不同分类的标志。很多标志指示Android系统如何启动一个活动(例如,活动应该属于哪个任务)以及它启动后如何处理它(例如,它是否属于最近活动的列表)。所有这些标志被定义在Intent类中。

 

The Android system and the applications that come with the platform employ Intent objects both to send out system-originated broadcasts and to activate system-defined components. To see how to structure an intent to activate a system component, consult the list of intents in the reference.

 

Android系统和伴随系统的应用程序使用Intent对象发送面向系统的广播并激活系统定义的组件。如果想知道如何构建一个意图以激活系统组件,请参照参考手册中的意图列表。

 

-------------------------------

 

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.

 

* 显式意图用名称指定目标组件(组件的name域,前面已提及,拥有一个值集合)。因为组件名称通常不会被其它应用程序的开发者知道,所以显式意图通常用于应用程序内部的消息——诸如一个活动启动一个从属服务或启动一个姐妹活动。

 

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

 

* 隐式意图不指定目标名称(指定应用程序名称的域是空白的)。隐式意图通常用于激活其它应用程序的组件。

 

Android delivers an explicit intent to an instance of the designated target class. Nothing in the Intent object other than the component name matters for determining which component should get the intent.

 

Android传递一个显式意图到指定目标类的实例。Intent对象中只有组件名决定应该获取意图的组件。

 

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. It does so by comparing the contents of the Intent object to intent filters, structures associated with components that can potentially receive intents. Filters advertise the capabilities of a component and delimit the intents it can handle. They open the component to the possibility of receiving implicit intents of the advertised type. If a component does not have any intent filters, it can receive only explicit intents. A component with filters can receive both explicit and implicit intents.

 

隐式意图需要不同的策略。在缺少指定目标时,Android系统必须找到最佳组件(或复数个组件)以处理意图——一个单一活动或服务执行请求动作,或者一组广播接收器响应广播公告。它通过比较意图对象的内容和意图过滤器,与组件关联的可以隐式接收意图的结构,来做到这一点,过滤器宣扬组件的功能并界定它可以处理的意图。它们开放组件使其可以接收所宣扬类型的隐式意图。如果一个组件没有任何意图过滤器,那么它只可以接收显式意图。一个带过滤器的组件既可以接收显式意图也可以接收隐式意图。

 

Only three aspects of an Intent object are consulted when the object is tested against an intent filter:

 

当对象(注:意图对象?)与意图过滤器进行测试时,Intent对象只有三个方面被考虑:

 

action 动作

data (both URI and data type)  数据(URI以及数据类型)

category 分类

 

The extras and flags play no part in resolving which component receives an intent.

 

extras和flags不参与解析哪个组件接收意图。

 

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.

 

为了通知系统它们可以处理哪个隐式意图,活动、服务和广播接收器可以拥有一个或多个意图过滤器。每个过滤器描述组件的一种功能,组件愿意接收的一组意图。它有效地过滤得到期待类型的意图,但过滤排除不想要意图——但只是不想要的隐式意图(那些没有命名目标类名的意图)。一个显式意图总是传递给它的目标,不管它包含什么;过滤器不被考虑。但一个隐式意图被传递给一个组件,仅当它可以传递通过其中一个组件的过滤器。

 

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

 

一个组件有不同的过滤器用于它可以做的各种工作,以及每个它可能呈现给用户的界面。例如,示例记事本应用程序的NoteEditor活动拥有两个过滤器——一个用于启动用户可以查看或编辑的特定便签,而另一个用于启动用户可以填充和保存的新空白便签。(所有记事本过滤器在后面的记事本示例章节中描述。)

 

-------------------------------

 

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.

 

出于安全考虑,一个意图过滤器是不可靠的。当它打开组件以仅接收某类隐式意图时,它无法阻止显式意图把组件作为目标。即便一个过滤器限制组件发出的意图以处理某些动作和数据源,有人总是能在一个显式意图中放进不同的动作和数据源,并指定组件名称作为目标。

 

-------------------------------

 

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

 

一个意图过滤器是一个IntentFilter类实例。然而,因为Android系统必须在它可以启动那个组件前知道关于组件的功能,所以意图过滤器通常不是在Java代码中配置,而是在应用程序清单文件中(AndroidManifest.xml)作为<intent-filter>元素。(一个例外是用于广播接收器的过滤器,它们通过调用Context.registerReceiver()来动态注册。它们被直接创建作为IntentFilter对象。)

 

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域。一个隐式意图与所有三个区域的过滤器测试。为了传递给拥有过滤器的组件,它必须通过所有的三种测试。只要它其中一项失败了,Android系统将不会传递它给组件——至少不是基于那个过滤器。然而,因为一个组件可以拥有多个意图过滤器,所以没有通过其中一个组件过滤器的意图可能会通过另一个。

 

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:

 

清单中的<intent-filter>元素列举动作作为<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.

 

正如示例所示,虽然一个Intent对象的只命名一个单一动作,但过滤器可以列举多于一个动作。列表不可以为空;一个过滤器必须包含至少一个<action>元素,否则它将拒绝所有意图。

 

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对象中指定的动作必须匹配列举在过滤器中的其中一个动作。如果该对象或过滤器不指定动作,结果将像如下那样:

 

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

 

* 如果过滤器无法列举任何动作,那么没有东西与意图匹配,所以所有意图将测试失败。没有意图可以通过过滤器。

 

* 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对象自动通过测试——只要过滤器包含至少一个动作。

 

* Category test

 

* 分类测试

 

An <intent-filter> element also lists categories as subelements. For example:

 

一个<intent-filter>元素还列举分类作为子元素。例如:

 

-------------------------------

 

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

 

注意前面描述动作和分类的常量没有用在清单文件。改为使用完全的字符串值。例如,在上面的示例中android.intent.category.BROWSABLE字符串对应本文前面提及的CATEGORY_BROWSABLE常量。类似地,字符串android.intent.action.EDIT对应ACTION_EDIT常量。

 

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对象中的每个分类必须匹配过滤器的一个分类。过滤器可以列出附加的分类,但它不能忽略意图中的任何分类。

 

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()的隐式意图,就像它们至少有一个分类:android.intent.category.DEFAULT(CATEGORY_DEFAULT常量)。因此,希望接收隐式意图的活动必须在它们的意图过滤器中包含android.intent.category.DEFAULT。(带有android.intent.action.MAIN和android.intent.category.LAUNCHER设置的过滤器是例外。它们标注开始新任务的活动并呈现在启动器屏幕上。它们可以包含android.intent.category.DEFAULT在分类列表中,但不是必须的。)想获取更多关于过滤器的信息,请参见后面使用意图匹配的章节。

 

* 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和数据类型(MIME媒体类型)。有独立的属性——scheme,host,port,和path——对应URI的各个部分:

 

scheme://host:port/path

 

For example, in the following URI,

 

例如,在以下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.

 

模式是content,宿主是com.example.project,端口是200,而路径是folder/subfolder/etc。宿主和端口一起构成URI权限;如果宿主没有指定,端口将被忽略。

 

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.

 

这些属性的每一个都是可选的,但它们相互独立:为了让权限有意义,必须同时指定模式。为了让路径有意义,必须同时指定模式和权限。

 

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.

 

当Intent对象中的URI与过滤器中指定的URI比较,它仅比较在过滤器中提及的URI的局部。例如,如果一个过滤器仅指定一个模式,那么所有带有那个模式的URI匹配过滤器。如果一个过滤器指定模式和权限但没有路径,那么所有带有相同模式和权限的URI可匹配,不管它们的路径是什么。如果一个过滤器指定一个模式,一个权限,以及一个路径,那么只有带有相同模式、权限、和路径的URI才可匹配。然而,过滤器中的路径指定可以包含通配符以指定只有一部分匹配的路径。

 

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.

 

<data>元素的type属性指定数据的MIME类型。它比URI更常出现在过滤器中。Intent对象和过滤器都可以对subtype域使用*通配符——例如,"text/*"或"audio/*"——指示任意的子类型匹配。

 

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:

 

数据测试比较Intent对象中的URI和数据类型和过滤器中指定的URI和数据类型。规则如下:

 

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

 

a. 一个既没有包含URI也没有包含数据类型的Intent对象,仅当过滤器也没有指定任何URI或数据类型时,才通过测试。

 

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

 

b. 包含URI但没有数据类型(以及不能从URI中推断的类型)的Intent对象,仅当它的URI匹配过滤器的URI而过滤器也没有指定类型时,才通过测试。这仅限于类似mailto:和tel:的URI的情况,它们不引用实际数据。

 

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

 

c. 一个包含数据类型但没有URI的Intent对象,仅当过滤器列举相同的数据类型并类似地不指定一个URI,才通过测试。

 

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

 

d. 既包含URI又包含数据类型(或可以从URI中推断的数据类型)的Intent对象通过测试的数据类型部分,仅当它的类型匹配在过滤器中列举的一个类型。只有它的URI匹配过滤器中的一个URI,或者它拥有content:或file: URI并且过滤器没有指定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.

 

如果一个意图可以通过多于一个活动或服务的过滤器,那么用户可能被询问要激活哪个组件。如果没有找到目标,会引发一个异常。

 

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:

 

展示在上面用于数据测试的最后规则,规则(d),反映一个例外,组件可以从一个文件或内容提供者中获取局部数据。因此,它们的过滤器可以只列举一个数据类型而不需要显式命名content:和file:模式。这是一种典型情况。例如,像下面那样子的<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:

 

另一种通常的配置是带有模式和数据类型的过滤器。例如,一个类似下面内容的<data>元素告诉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.

 

例如,考虑在用户跟随网页中的链接时浏览器应用程序做的事情。它首先尝试显示数据(正如如果链接是一个HTML页面它所做的那样)。如果它不能显示数据,它在一个隐式意图中放进模式和数据类型并尝试启动一个可以做该工作的活动。如果没有接受者,它询问下载管理器下载数据。那样会把它放置在一个内容提供者的控制下,使一个隐式的更大的活动池(那些带有只命名数据类型的过滤器的活动)可以响应它。

 

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:

 

大多数应用程序还拥有一种重新启动的方式,不需要引用任何特定数据。可以初始化应用程序的活动拥有带指定android.intent.action.MAIN动作的过滤器。如果它们准备呈现在应用程序启动器中,它们还要指定android.intent.category.LAUNCHER分类:

 

-------------------------------

 

<intent-filter . . . >

    <action android:name="code android.intent.action.MAIN" />

    <category android:name="code android.intent.category.LAUNCHER" />

</intent-filter>

 

-------------------------------

 

Using intent matching

 

使用意图匹配

 

Intents are matched against intent filters not only to discover a target component to activate, but also to discover something about the set of components on the device. For example, the Android system populates the application launcher, the top-level screen that shows the applications that are available for the user to launch, by finding all the activities with intent filters that specify the "android.intent.action.MAIN" action and "android.intent.category.LAUNCHER" category (as illustrated in the previous section). It then displays the icons and labels of those activities in the launcher. Similarly, it discovers the home screen by looking for the activity with "android.intent.category.HOME" in its filter.

 

意图被意图过滤器匹配不只是为了发现要激活的目标组件,还为了发现设备上的组件集合的相关东西。例如,Android系统通过查找所有带有指定android.intent.action.MAIN动作和android.intent.category.LAUNCHER分类的意图过滤器的活动,生成应用程序启动器,即显示可以被用户启动的应用程序的顶级屏幕(正如前面章节描述的那样)。然后它在启动器中显示那些活动的图标和标签。类似地,它通过查找在其过滤器中带android.intent.category.HOME的活动,发现桌面(注:主页)屏幕。

 

Your application can use intent matching is a similar way. The PackageManager has a set of query...() methods that return all components that can accept a particular intent, and a similar series of resolve...() methods that determine the best component to respond to an intent. For example, queryIntentActivities() returns a list of all activities that can perform the intent passed as an argument, and queryIntentServices() returns a similar list of services. Neither method activates the components; they just list the ones that can respond. There's a similar method, queryBroadcastReceivers(), for broadcast receivers.

 

你的应用程序可以用类似的方法使用意图匹配(注:这里is疑有误)。PackageManager拥有一组query...()方法返回所有可接受特定意图的组件,以及一组类似resolve...()方法决定响应意图的最佳组件。例如queryIntentActivities()返回可以执行传递的意图作为参数的所有活动的列表,而queryIntentServices()返回一个类似的服务列表。这两种方法都不能激活组件;它们只是列举可以响应的组件。有类似的方法,queryBroadcastReceivers(),用于广播接收器。

 

Note Pad Example

 

记事本示例

 

The Note Pad sample application enables users to browse through a list of notes, view details about individual items in the list, edit the items, and add a new item to the list. This section looks at the intent filters declared in its manifest file. (If you're working offline in the SDK, you can find all the source files for this sample application, including its manifest file, at <sdk>/samples/NotePad/index.html. If you're viewing the documentation online, the source files are in the Tutorials and Sample Code section here.)

 

记事本示例应用程序使用户能浏览整个便签列表,查看关于列表中单个条目的细节,编辑条目,并添加新的条目到列表。本章节把目光放在它的清单文件中声明的意图过滤器。(如果你正在离线使用SDK工作,你可以查找这个示例应用程序的所有源文件,包括它的清单文件,在<sdk>/samples/NotePad/index.html。如果你正在在线查看文档,源文件在这里的教程和示例代码章节中。)

 

In its manifest file, the Note Pad application declares three activities, each with at least one intent filter. It also declares a content provider that manages the note data. Here is the manifest file in its entirety:

 

在它的清单文件中,记事本应用程序声明三个活动,每个活动带有至少一个意图过滤器。它还声明一个管理便签数据的内容提供者。这里是整个清单文件:

 

-------------------------------

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

          package="com.example.android.notepad">

    <application android:icon="@drawable/app_notes"

                 android:label="@string/app_name" >

 

        <provider android:name="NotePadProvider"

                  android:authorities="com.google.provider.NotePad" />

 

        <activity android:name="NotesList" android:label="@string/title_notes_list">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-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-filter>

                <action android:name="android.intent.action.GET_CONTENT" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

            </intent-filter>

        </activity>

 

        <activity android:name="NoteEditor"

                  android:theme="@android:style/Theme.Light"

                  android:label="@string/title_note" >

            <intent-filter android:label="@string/resolve_edit">

                <action android:name="android.intent.action.VIEW" />

                <action android:name="android.intent.action.EDIT" />

                <action android:name="com.android.notepad.action.EDIT_NOTE" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

            </intent-filter>

            <intent-filter>

                <action android:name="android.intent.action.INSERT" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

            </intent-filter>

        </activity>

 

        <activity android:name="TitleEditor" 

                  android:label="@string/title_edit_title"

                  android:theme="@android:style/Theme.Dialog">

            <intent-filter android:label="@string/resolve_title">

                <action android:name="com.android.notepad.action.EDIT_TITLE" />

                <category android:name="android.intent.category.DEFAULT" />

                <category android:name="android.intent.category.ALTERNATIVE" />

                <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />

                <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

            </intent-filter>

        </activity>

 

    </application>

</manifest>

 

-------------------------------

 

The first activity, NotesList, is distinguished from the other activities by the fact that it operates on a directory of notes (the note list) rather than on a single note. It would generally serve as the initial user interface into the application. It can do three things as described by its three intent filters:

 

第一个活动,NotesList,通过它操作在便签目录(便签列表)而非操作在单一便签的事实,与其它活动区分。它通常担当进入应用程序的最初用户界面。它可以做三件事,正如它的三个意图过滤器描述的那样:

 

-------------------------------

 

<intent-filter>

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

 

-------------------------------

 

This filter declares the main entry point into the Note Pad application. The standard MAIN action is an entry point that does not require any other information in the Intent (no data specification, for example), and the LAUNCHER category says that this entry point should be listed in the application launcher.

 

这个过滤器声明进入记事本应用程序的主入口点。标准的MAIN动作是一个入口点,它不需要意图中其它任何信息(例如,没有数据指定),而LAUNCHER分类表示这个入口点应该被列举在应用程序启动器中。

 

-------------------------------

 

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

 

-------------------------------

 

This filter declares the things that the activity can do on a directory of notes. It can allow the user to view or edit the directory (via the VIEW and EDIT actions), or to pick a particular note from the directory (via the PICK action).

 

过滤器声明活动可以在一个便签目录上做的事情。它可以允许用户查看或编辑目录(通过VIEW和EDIT动作),或从目录中拾取一个特定的便签(通过PICK动作)。

 

The mimeType attribute of the <data> element specifies the kind of data that these actions operate on. It indicates that the activity can get a Cursor over zero or more items (vnd.android.cursor.dir) from a content provider that holds Note Pad data (vnd.google.note). The Intent object that launches the activity would include a content: URI specifying the exact data of this type that the activity should open.

 

<data>元素的mimeType属性指定这些动作操作在的数据类型。它指示活动可以获取来自持有记事本数据(vnd.google.note)的内容提供者的零个或更多条目(vnd.android.cursor.dir)上的Cursor对象。启动活动的Intent对象应该包含指定活动应该打开的此类确切数据的content: URI。

 

Note also the DEFAULT category supplied in this filter. It's there because the Context.startActivity() and Activity.startActivityForResult() methods treat all intents as if they contained the DEFAULT category — with just two exceptions:

 

还要注意的是在这个过滤器中提供的DEFAULT分类。出现在那里是因为Context.startActivity()和Activity.startActivityForResult()方法对待所有意图就像它们包含DEFAULT分类一样——但只有两个例外:

 

* Intents that explicitly name the target activity

 

* 显式命名目标活动的意图

 

* Intents consisting of the MAIN action and LAUNCHER category

 

* 由MAIN动作和LAUNCHER分类组成的意图

 

Therefore, the DEFAULT category is required for all filters — except for those with the MAIN action and LAUNCHER category. (Intent filters are not consulted for explicit intents.)

 

因此,DEFAULT是所有过滤器所需的——除非那些带有MAIN动作和LAUNCHER分类的过滤器。(意图过滤器不考虑显式意图)

 

-------------------------------

 

<intent-filter>

    <action android:name="android.intent.action.GET_CONTENT" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

 

-------------------------------

 

This filter describes the activity's ability to return a note selected by the user without requiring any specification of the directory the user should choose from. The GET_CONTENT action is similar to the PICK action. In both cases, the activity returns the URI for a note selected by the user. (In each case, it's returned to the activity that called startActivityForResult() to start the NoteList activity.) Here, however, the caller specifies the type of data desired instead of the directory of data the user will be picking from.

 

这个过滤器描述活动有能力返回一个被用户选择的便签而不要求用户应该选择某个指定的目录。GET_CONTENT动作类似PICK动作。在两种情况下,活动返回被用户选择的便签的URI。(在每种情况下,它返回给调用startActivityForResult()启动NoteList活动的活动。)然而,这里调用方指定期待的数据类型而非用户将要从中拾取的数据目录。

 

The data type, vnd.android.cursor.item/vnd.google.note, indicates the type of data the activity can return — a URI for a single note. From the returned URI, the caller can get a Cursor for exactly one item (vnd.android.cursor.item) from the content provider that holds Note Pad data (vnd.google.note).

 

数据类型,vnd.android.cursor.item/vnd.google.note,指示活动可以返回的数据类型——单一便签的URI。从返回的URI中,调用方可以从持有记事本数据(vnd.google.note)的内容提供者中获取指向正好一条条目(vnd.android.cursor.item)的Cursor对象。

 

In other words, for the PICK action in the previous filter, the data type indicates the type of data the activity could display to the user. For the GET_CONTENT filter, it indicates the type of data the activity can return to the caller.

 

换言之,对于前面过滤器中的PICK动作,数据类型指示活动显示给用户的数据类型。对于GET_CONTENT过滤器,它指示活动可以返回给调用方的数据类型。

 

Given these capabilities, the following intents will resolve to the NotesList activity:

 

考虑这些功能,以下意图将被解析到NotesList活动:

 

action: android.intent.action.MAIN

 

Launches the activity with no data specified.

 

启动活动,不指定数据。

 

action: android.intent.action.MAIN 

category: android.intent.category.LAUNCHER

 

Launches the activity with no data selected specified. This is the actual intent used by the Launcher to populate its top-level list. All activities with filters that match this action and category are added to the list.

 

启动活动,不指定选择的数据。这是被Launcher使用的实际意图以生成它的顶级列表。所有带有匹配此动作和分类过滤器的活动被添加到列表。

 

action: android.intent.action.VIEW 

data: content://com.google.provider.NotePad/notes

 

Asks the activity to display a list of all the notes under content://com.google.provider.NotePad/notes. The user can then browse through the list and get information about the items in it.

 

叫活动显示在content://com.google.provider.NotePad/notes下的所有便签。然后用户可以浏览这个列表并获取关于其中条目的信息。

 

action: android.intent.action.PICK 

data: content://com.google.provider.NotePad/notes

 

Asks the activity to display a list of the notes under content://com.google.provider.NotePad/notes. The user can then pick a note from the list, and the activity will return the URI for that item back to the activity that started the NoteList activity.

 

叫活动显示content://com.google.provider.NotePad/notes下的所有便签。然后用户可以从列表中选择一条便签,而活动将把那个条目的URI返回给启动NoteList活动的活动。

 

action: android.intent.action.GET_CONTENT 

data type: vnd.android.cursor.item/vnd.google.note

 

Asks the activity to supply a single item of Note Pad data.

 

叫活动提供记事本数据的单一条目。

 

The second activity, NoteEditor, shows users a single note entry and allows them to edit it. It can do two things as described by its two intent filters:

 

第二个活动,NoteEditor,向用户展示一个单一便签条目并允许他们编辑它。它可以做两件事情正如它的两个意图过滤器描述的那样。

 

-------------------------------

 

<intent-filter android:label="@string/resolve_edit">

    <action android:name="android.intent.action.VIEW" />

    <action android:name="android.intent.action.EDIT" />

    <action android:name="com.android.notepad.action.EDIT_NOTE" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

 

-------------------------------

 

The first, primary, purpose of this activity is to enable the user to interact with a single note — to either VIEW the note or EDIT it. (The EDIT_NOTE category is a synonym for EDIT.) The intent would contain the URI for data matching the MIME type vnd.android.cursor.item/vnd.google.note — that is, the URI for a single, specific note. It would typically be a URI that was returned by the PICK or GET_CONTENT actions of the NoteList activity.

 

这个活动的首要和主要目的是使用户能与单个便签交互——不管是查看便签还是编辑它。(EDIT_NOTE分类是EDIT的近义词)。意图将包含匹配MIME类型vnd.android.cursor.item/vnd.google.note的数据的URI——即表示单一指定便签的URI。通常它是由NoteList活动的PICK或GET_CONTENT动作返回的URI。

 

As before, this filter lists the DEFAULT category so that the activity can be launched by intents that don't explicitly specify the NoteEditor class.

 

正如前面所说,过滤器列出DEFAULT分类,使活动可以被没有显式指定NoteEditor类的意图启动。

 

-------------------------------

 

<intent-filter>

    <action android:name="android.intent.action.INSERT" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />

</intent-filter>

 

-------------------------------

 

The secondary purpose of this activity is to enable the user to create a new note, which it will INSERT into an existing directory of notes. The intent would contain the URI for data matching the MIME type vnd.android.cursor.dir/vnd.google.note — that is, the URI for the directory where the note should be placed.

 

这个活动的第二目的是使用户能创建一个新便签,它将插入到一个现存的便签目录中。意图将包含匹配MIME类型vnd.android.cursor.dir/vnd.google.note的数据URI——即,应该放置便签的目录的URI。

 

Given these capabilities, the following intents will resolve to the NoteEditor activity:

 

考虑这些功能,以下意图将解析到NoteEditor活动。

 

action: android.intent.action.VIEW 

data: content://com.google.provider.NotePad/notes/ID

 

Asks the activity to display the content of the note identified by ID. (For details on how content: URIs specify individual members of a group, see Content Providers.)

 

叫活动显示ID标识的标签的内容。(想获取关于content: URI如何指定组的独立成员的细节,请参见内容提供者。)

 

action: android.intent.action.EDIT 

data: content://com.google.provider.NotePad/notes/ID

 

Asks the activity to display the content of the note identified by ID, and to let the user edit it. If the user saves the changes, the activity updates the data for the note in the content provider.

 

叫活动显示由ID标识的便签的内容,并让用户编辑它。如果用户保存这些改变,活动更新内容提供者内的便签数据。

 

action: android.intent.action.INSERT 

data: content://com.google.provider.NotePad/notes

 

Asks the activity to create a new, empty note in the notes list at content://com.google.provider.NotePad/notes and allow the user to edit it. If the user saves the note, its URI is returned to the caller.

 

叫活动在content://com.google.provider.NotePad/notes标签列表中创建一个新的,空的便签并允许用户编辑它。如果用户保存便签,它的URI被返回给调用方。

 

The last activity, TitleEditor, enables the user to edit the title of a note. This could be implemented by directly invoking the activity (by explicitly setting its component name in the Intent), without using an intent filter. But here we take the opportunity to show how to publish alternative operations on existing data:

 

最后的活动,TitleEditor,使用户能编辑便签的标题。它可以通过直接调用活动来实现(通过在Intent对象中显式设置它的组件名),不需要使用意图过滤器。但这里我们用这个机会展示如何在现存数据上发布可选操作:

 

-------------------------------

 

<intent-filter android:label="@string/resolve_title">

    <action android:name="com.android.notepad.action.EDIT_TITLE" />

    <category android:name="android.intent.category.DEFAULT" />

    <category android:name="android.intent.category.ALTERNATIVE" />

    <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />

    <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />

</intent-filter>

 

-------------------------------

 

The single intent filter for this activity uses a custom action called "com.android.notepad.action.EDIT_TITLE". It must be invoked on a specific note (data type vnd.android.cursor.item/vnd.google.note), like the previous VIEW and EDIT actions. However, here the activity displays the title contained in the note data, not the content of the note itself.

 

这个活动的单一意图过滤器使用一个称为com.android.notepad.action.EDIT_TITLE的定制动作。它必须调用在一个特定的便签上(数据类型vnd.android.cursor.item/vnd.google.note),像前面的VIEW和EDIT动作那样。然而,这里活动显示包含在便签数据中的标题,而非便签自身的内容。

 

In addition to supporting the usual DEFAULT category, the title editor also supports two other standard categories: ALTERNATIVE and SELECTED_ALTERNATIVE. These categories identify activities that can be presented to users in a menu of options (much as the LAUNCHER category identifies activities that should be presented to user in the application launcher). Note that the filter also supplies an explicit label (via android:label="@string/resolve_title") to better control what users see when presented with this activity as an alternative action to the data they are currently viewing. (For more information on these categories and building options menus, see the PackageManager.queryIntentActivityOptions() and Menu.addIntentOptions() methods.)

 

除了支持通常的DEFAULT分类外,标题编辑器还支持其他两种标准分类:ALTERNATIVE和SELECTED_ALTERNATIVE。这两个分类指定活动为可以在选项菜单中呈现给用户(尽管LAUNCHER分类指定活动应该在应用程序启动器中呈现给用户)。注意过滤器还提供一个显式的标签(通过android:label="@string/resolve_title")以更好地控制用户看到的东西,当它与活动一起呈现作为一个可选动作提供给他们当前在查看的数据时。(想获取更多关于这些分类和构建选项菜单的信息,请参见PackageManager.queryIntentActivityOptions()和Menu.addIntentOptions()方法。)

 

Given these capabilities, the following intent will resolve to the TitleEditor activity:

 

考虑这些功能,以下意图将解析到TitleEditor活动:

 

action: com.android.notepad.action.EDIT_TITLE 

data: content://com.google.provider.NotePad/notes/ID

 

Asks the activity to display the title associated with note ID, and allow the user to edit the title.

 

叫活动显示与便签ID相关联的标题,并允许用户编辑标题。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 04 Nov 2011 0:15

 

Android 4.0 r1 - 29 Mar 2012 18:25

 

-------------------------------

 

patch:

1. Android 4.0 r1 - 29 Mar 2012 18:25

(1)

when the HOME key is pressed.

->

when the Home button is pressed.

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)


分享到:
评论

相关推荐

    Android意图和意图过滤器

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

    意图与意图过滤器.ppt

    Android意图与意图过滤器

    【Android开发API】应用的组成部分-意图和意图过滤器.pdf

    【Android开发API】应用的组成部分-意图和意图过滤器.pdf

    IntentFilter:使用意图过滤器的简单示例

    意图过滤器 使用意图过滤器的简单示例

    SRM_BillingAppCalculator:意图过滤器

    SRM_BillingAppCalculator 意图过滤器

    Android开发指南

    意图和意图过滤器Intents and Intent Filters 一个应用程序的三个核心组件-活动,服务和广播接收器是通过...对于没有显式命名一个目标组件的意图,这个过程包括对照与潜在目标相关联的意图过滤器来测试这个意图对象。

    Android初学习之intent-filter意图过滤器

    浅谈 intent filter对activity组件的应用 Intent Filter就是用来注册 Activity 、Service 、 Broadcast、Receiver(四大组件) 具有能在某种数据上执行一个动作的能力。使用 Intent Filter ,应用程序组件告诉 ...

    cordova-share-plugin:创建一个意图过滤器,让你的 AppGyver 应用听到分享事件

    创建一个意图过滤器,让你的 AppGyver 应用听到分享事件 添加到科尔多瓦: cordova plugin add https://github.com/therealplato/cordova-share-plugin.git 添加到 Appgyver 应用程序: 配置构建时,将此 repo ...

    Android开发指南中文版

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

    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 ? 系统偏好:快速...

    JSP使用过滤器防止SQL注入的简单实现

    具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。...

    FilteredIntent:Android意向过滤器库

    您可以添加过滤器以选择应用程序。 过滤器是应用程序名称和程序包名称。 您可以更轻松地显示选择对话框。 Gradle和Maven Gradle dependencies { implementation ' net.sjava:filteredintent:1.3.0 ' } Maven...

    android模拟器系统源码

    android开发所使用的 模拟器系统源码,用于练习时需要跳转到模拟器自带页面时 ,设置隐式意图 , 查看意图过滤器等等。ceshiguo eclipse自带 AVD模拟器 和 genymotion模拟器

    Android_SDK_Developper_Guide_中文版.zip

    应用程序组件 Activity和任务 进程和线程 组件生命周期 调用父类 用户界面UserInterface ...意图和意图过滤器 Intent and InetentFilters 数据存储DataStorage 内容提供器ContentProvider 清单文件

    Android 隐式Intent的实例详解

    Android 隐式Intent的实例详解 前言: 顾名思义,隐式意图就是在不明确设置激活对象的...在intent过滤器中类似于上面例子中的”身高“条件的匹配条件有: (1)action (2)category (3)data:scheme、host、path、typ

    weebtv:XMTV播放器的WeebTV插件

    在AndroidManifest.xml中用您的名称更改意图过滤器动作名称,它必须将此新名称复制到元数据值。 更改前: &lt;意图过滤器&gt; “&gt; &lt;/ intent&gt; &lt;元数据android:name =“ xmtvplugin” android:value =“ ...

    Android开发指南中文版.pdf

    Android Android开发指南中文版 本PDF是Android开发指南的中文版,介绍了应用基础,视图层次,布局,邮件,用户界面事件,菜单,高级话题,资源和资产,意图和意图过滤器,数据存储,内容提供器,简单文件等知识

Global site tag (gtag.js) - Google Analytics