原文:http://su1216.iteye.com/blog/1609261
Intents and Intent Filters
Intents和Intent Filters
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:
应用的3大核心组件 - activity,service和broadcast receiver - 通过信息激活,被称之为intent。
Intent消息是运行时后期在相同或不同应用中绑定组件的便捷工具。
intent本身,一个Intent对象,是一个持有要执行操作的抽象描述(或者,经常在broadcast情况中,已经发生并且正在宣布的事情的一个描述)的可传递的数据结构
分发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或者获得一个已经存在的activity来做一些新的事情(也可以是传到Activity.setResult()来返回信息给调用startActivityForResult()的activity)
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()来初始化一个service或者给一个运行的service发布新的指令
简单的说,一个intent可以传递给Context.bindService()来建立一个调用者组件和目标service之间的连接。
如果它尚未运行,它可以选择启动服务。
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.
Intent对象传递到任何广播方法中(比如Context.sendBroadcast(), Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast())向所有感兴趣的broadcast receiver发布。
很多各种各样的广播在系统代码中产生。
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系统找到适合的activity,service或者broadcast receiver集合来响应intent,如果有必要的话实例化他们。
这些信息系统内没有重叠部分:广播intent仅会被发送到broadcast receiver,不会发送到activity和service
传递给startActivity()之后发送到一个activity中,不会发到service或者broadcast receiver……
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消息
对于隐式的intent来说,这个过程相对于intent过滤器包含关联潜在的目标测试intent对象。
Intent Objects
Intent对象
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对象是一捆信息
它包含收到intent的组件感兴趣的信息(比如要采取的行为和遵循的数据)加上感兴趣的Android系统信息(比如应该处理intent和指令如何启动一个目标activity的组件的种类)
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.
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.
The component name is set by setComponent(), setClass(), or setClassName() and read by getComponent().
应该处理intent组件的名称
这个字段是ComponentName对象 - 目标组件的全限定类名(例如:"com.example.project.app.FreneticActivity")和在manifest文件中设置的应用包名(组件驻留的地方)的一个组合(例如"com.example.project")
组件名称的包部分和在manifest中设置的包名不需要匹配。
组件名称是可选的
如果设置了,Intent对象被发送到指定类的一个实例。
如果没有设置,Android使用其他在Intent对象中的信息类定位一个合适的目标 - 参看Intent解析,此文档的后面。
组件名称通过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:
一个字符串命名的行为被执行 - 或者在broadcast intent事例中,发生过的并且正被报告的行为
Intent类定义了一些行为常量,包括:
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. |
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.
action主要决定了intent其余部分是如何构造的 - 尤其是数据和附加域部分 - 这很像一个方法名决定一组实参和一个返回值
由于这个原因,使用action并尽量指定其name是一个好的办法,为了紧密的把他们连接到intent其他域中。
换句话说,为你组件要处理的Intent定义一个完整的协议而不是单独的定义一个action
The action in an Intent object is set by the setAction() method and read by getAction().
Intent对象中的action通过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为ACTION_EDIT,data会包含要显示编辑的文档的URI
如果action为ACTION_CALL,data会包含一个电话号码:带有要拨打的号码的URI
相似的,如果action为ACTION_VIEW并且data域为一个http:URI,接收它的activity不论data中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.
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().
当一个intent与一个有能力处理data的组件匹配时,除了它的URI之外,了解data的类型(MIME类型)总是很重要的。
例如:一个能够显示图片数据组件不应该用来播放音频文件。
很多例子中,data类型可以从URI推测出 - 特别是content:URI,指出data在设备上的位置并且由content provider控制(参看content provider独立的讨论)。
但是类型也可以显式的设置在Intent对象中。
setData()方法指定data只作为URI,setType()指定其只作为一个MIME类型,setDataAndType()同时指定其为URI和MIME类型。
通过getData()读取URI,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的这类组件的附加信息。
任何数量的Category描述可以放置到一个Intent对象中。
就像为action做的一样,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_GADGET | The activity can be embedded inside of another activity that hosts gadgets. |
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_LAUNCHER | The activity can be the initial activity of a task and is listed in the top-level application launcher. |
CATEGORY_PREFERENCE | The target activity is a preference panel. |
See the Intent class description for the full list of categories.
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.
参见Intent类描述关于Category的完整列表
addCategory()方法放置一个category到Intent对象,removeCategory()删除一个之前添加的category,getCategories()获得当前对象中所有category的集合
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.
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的组件中的附加数据键值对
就像一些action与指定的数据
例如:一个ACTION_TIMEZONE_CHANGED intent有一个识别新时区的"time-zone"附加数据,ACTION_HEADSET_PLUG有一个"state"附加数据指示现在耳机有没有插入,和一个耳机状态类型的“name”附加数据一样。
如果你想要创建一个SHOW_COLOR行为,颜色值应该设置在附加的键值对中。
Intent对象有一系列的put...()方法来插入各种类型的附加数据和相似的与get...()对应的set方法来读取数据
这些方法与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.
各种flag
很多flag指示Android系统如何启动一个activity(例如:activity应该属于哪一个task)和在它启动之后如何看待它(比如:它是否属于最近activity列表中)
所有这些flag都定义在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来激活一个系统组件参考intent说明列表。
Intents List: Invoking Google Applications on Android Devices
Intent列表:在Android设备上执行Google应用
The table below lists the intents that your application can send, to invoke Google applications on Android devices in certain ways.
For each action/uri pair, the table describes how the receiving Google application handles the intent.
你的应用可以发送下面表格中intent一某中方式来执行Android设备上Google应用。
对于每一个action/uri对,表格描述了接收的Google应用如何处理intent
Note that this list is not comprehensive.
注意:这个列表并不完整。
(表格似乎显式有些问题,原表格地址:http://developer.android.com/guide/appendix/g-app-intents.html)
Target Application | Intent URI | Intent Action | Result | ||||||||||||
Browser | http://web_address https://web_address | VIEW | Open a browser window to the URL specified. | ||||||||||||
"" (empty string) http://web_address https://web_address | WEB_SEARCH | Opens the file at the location on the device in the browser. | |||||||||||||
Dialer | tel: phone_number | CALL | Calls the entered phone number. Valid telephone numbers as defined in the IETF RFC 3966 are accepted. Valid examples include the following:
The dialer is good at normalizing some kinds of schemes: for example telephone numbers, so the schema described isn't strictly required in the Note: This requires your application to request the following permission in your manifest: | ||||||||||||
tel:phone_number | DIAL | Dials (but does not actually initiate the call) the number given (or the stored voicemail on the phone). Telephone number normalization described for CALL applies to DIAL as well. | |||||||||||||
Google Maps | geo:latitude,longitude geo:latitude,longitude?z=zoom geo:0,0?q=my+street+address geo:0,0?q=business+near+city | VIEW | Opens the Maps application to the given location or query. The Geo URI scheme (not fully supported) is currently under development. The z field specifies the zoom level. A zoom level of 1 shows the whole Earth, centered at the given lat,lng. A zoom level of 2 shows a quarter of the Earth, and so on. The highest zoom level is 23. A larger zoom level will be clamped to 23. | ||||||||||||
Google Streetview | google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom | VIEW | Opens the Street View application to the given location. The URI scheme is based on the syntax used for Street View panorama information in Google Maps URLs. The cbll field is required. The cbp and mz fields are optional.
|
原文地址如下,英文水平实在有限,希望拍砖同时能给予指正。
http://developer.android.com/guide/components/intents-filters.html
http://developer.android.com/guide/appendix/g-app-intents.html
转贴请保留以下链接
blog地址