Intents and Intent Filters (一)

来源:互联网 发布:端口110 编辑:程序博客网 时间:2024/06/06 00:41

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

ntents and Intent Filters

In this document

  1. Intent Objects
  2. Intent Resolution
  3. Intent filters
  4. Common cases
  5. Using intent matching
  6. Note Pad Example

Key classes

  1. Intent
  2. IntentFilter
  3. BroadcastReceiver
  4. 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 Intentobject, 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:

  • An Intent object is passed to Context.startActivity() orActivity.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().)
  • 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 toContext.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 objects passed to any of the broadcast methods (such as Context.sendBroadcast()Context.sendOrderedBroadcast(), orContext.sendStickyBroadcast()) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code.

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.

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 againstintent filters associated with potential targets.


意图和意图过滤器

在这份文件中

  1. 意图对象
  2. 意向书的决议
  3. 意图过滤器
  4. 常见的情况
  5. 使用意图匹配
  6. 注意垫为例

重点班

  1. 意图
  2. IntentFilter
  3. BroadcastReceiver
  4. 套装软体

三是激活应用程序的核心组件-活动,服务和广 ​​播接收机-通过邮件,称为意图。消息意图是后期运行时组件之间的结合,在相同或不同的应用设施。本身的意图,一个 Intent 对象,是一个被动的数据结构要执行的操作的抽象描述-或者,经常在广播节目的情况下,事情已经发生和正在公布的说明。有独立机制,为每个组件类型提供意向:

  • 一个Intent对象被传递给Context.startActivity()Activity.startActivityForResult()推出一项活动或现有活动做新的东西。(也可以通过为 Activity.setResult() 的活动,称为startActivityForResult()返回的信息。)
  • 一个Intent对象传递给Context.startService()来启动服务,或提供新的指令,一个持续的服务。同样,一个意图可以传递给Context.bindService()调用组件和目标服务之间建立一个连接。如果它尚未运行,它可以选择启动服务。

  • 意图对象传递给任何广播方法(如Context.sendBroadcast() Context.sendOrderedBroadcast() ,或Context.sendStickyBroadcast的() )交付给所有感兴趣的广播接收机。许多种类的广播起源于系统代码。

在每一种情况下,Android系统找到适当的活动,服务,或一套广播接收机的意图作出回应,如果有必要实例化他们。在这些邮件系统有没有重叠:广播意图提供广播接收机,从不活动或服务。交付的意图传递给 startActivity()的唯一一项活动,从来没有一个服务或广播接收机,等等。

这个文件开始与意向书对象的描述。然后介绍了Android使用的规则映射组件的意图-如何解决这部分应该得到的意图消息。对于不明确一个目标组件命名的意图,这个过程涉及到测试对Intent对象的 意图过滤器与潜在目标。

转http://www.cnblogs.com/skynet/archive/2010/07/20/1781644.html

一个应用程序的三个核心组件——activitiesservicesbroadcast receivers,都是通过叫做intents的消息激活。Intent消息是一种同一或不同应用程序中的组件之间延迟运行时绑定的机制。intent本身(是一个Intent对象),是一个被动的数据结构保存一个将要执行的操作的抽象描述,或在广播的情况下,通常是某事已经发生且正在宣告。对于这三种组件,有独立的传送intent的机制:

  • Activity:一个intent对象传递给Context.startActivity()Activity.startActivityForRestult()去启动一个活动或使一个已存在的活动去做新的事情。
  • Service:一个intent对象传递给Context.startService()去初始化一个service或传递一个新的指令给正在运行的service。类似的,一个intent可以传递给Context.bindService()去建立调用组件和目标服务之间的连接。
  • Broadcast Receiver:一个intent对象传递给任何广播方法(如Context.sendBroadcast()Context.sendOrderedBroadcast()Context.sendStickyBroadcast()),都将传递到所有感兴趣的广播接收者。

在每种情况下,Android系统查找合适的activity、service、broadcast receivers来响应意图,如果有必要的话,初始化他们。这些消息系统之间没有重叠,即广播意图仅会传递给广播接收者,而不会传递活动或服务,反之亦然。

下面首先描述intent对象,然后介绍Android将intent映射到相应组件的规则——如何解决哪个组件应该接收intent消息。对于没有指定目标组件名字的intent,这个处理过程包括按照intent filters匹配每个潜在的目标对象。


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:

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

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:

ConstantTarget componentActionACTION_CALLactivityInitiate a phone call.ACTION_EDITactivityDisplay data for the user to edit.ACTION_MAINactivityStart up as the initial activity of a task, with no data input and no returned output.ACTION_SYNCactivitySynchronize data on a server with data on the mobile device.ACTION_BATTERY_LOWbroadcast receiverA warning that the battery is low.ACTION_HEADSET_PLUGbroadcast receiverA headset has been plugged into the device, or unplugged from it.ACTION_SCREEN_ONbroadcast receiverThe screen has been turned on.ACTION_TIMEZONE_CHANGEDbroadcast receiverThe 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".

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.

The action in an Intent object is set by the setAction() method and read by 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.

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

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:ConstantMeaningCATEGORY_BROWSABLEThe 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_GADGETThe activity can be embedded inside of another activity that hosts gadgets.CATEGORY_HOMEThe activity displays the home screen, the first screen the user sees when the device is turned on or when the Homebutton is pressed.CATEGORY_LAUNCHERThe activity can be the initial activity of a task and is listed in the top-level application launcher.CATEGORY_PREFERENCEThe 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.

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.

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.

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.

意图对象

一个 Intent 对象是一个信息包。它包含感兴趣的信息接收组件的意图(例如,要采取的行动和行动的数据),加上Android系统感兴趣的信息(如组成部分,应该处理的意图和指示类别如何启动一个目标活动)。主要是,它可以包含以下内容:

组件名称
组件的名称,应该处理的意图。这个领域是一个ComponentName对象-一个目标组件的完全限定类名(例如“组合com.example.project.app.FreneticActivity “)和组件所在的应用程序清单文件中设置包名称(例如,“ com.example.project “)。组件的名称和包名称清单的一揽子计划的一部分不一定要匹配。

组件的名称是可选的。如果它被设置,Intent对象被传递到指定的类的一个实例。如果没有设置,Android使用的Intent对象的其他信息,以找到一个合适的目标-看到意向书的决议,在本文档后面。

组件名称设置由setComponent() , setClass。() ,或setClassName()和读由getComponent() 

Action
命名将要执行的动作 - 在广播意图的情况下,一个字符串,行动发生和正在报道。意图类定义了一些行动常量,包括这些:

常数目标组件行动ACTION_CALL活动发起一个电话。ACTION_EDIT活动为用户显示的数据进行编辑。ACTION_MAIN活动启动任务的初始活性,没有数据的输入,并没有返回的输出。ACTION_SYNC活动与移动设备上的数据在服务器上同步数据。ACTION_BATTERY_LOW广播接收机电池是低的一个警告。ACTION_HEADSET_PLUG广播接收机耳麦已经插入的设备,或者从它拔掉。ACTION_SCREEN_ON广播接收机屏幕已打开。ACTION_TIMEZONE_CHANGED广播接收机时区设置已经改变。

看到Intent通用行动的预定义的常量列表的类描述。别处定义在Android API的其他行动。你还可以定义自己的行动字符串激活应用程序中的组件。你发明的,应包括应用程序包作为前缀-例如:“ com.example.project.SHOW_COLOR “。

的行动在很大程度上决定其余的意图是结构性的-特别是data andextras领域-就像一个方法的名称确定一组参数和返回值。出于这个原因,它是一个好主意,使用尽可能具体的行动名称,情侣他们紧紧的意图等领域。换句话说,定义隔离行动,而不是定义为您的组件可以处理的Intent对象一个完整的协议。

在一个Intent对象的行动是由的 setAction() 方法和通过阅读 getAction() 

Data
担任数据的URI和MIME类型的数据。搭配不同类型的数据规格不同的动作。例如,行动领域ACTION_EDIT,数据字段包含的显示要编辑的文档的URI。如果这个动作是ACTION_CALL,将数据字段的一个电话:数量的URI调用。同样,如果行动是ACTION_VIEW和数据字段是一个HTTP: URI,接收活动将呼吁下载并显示URI是指任何数据。

当一个组件,它是能够处理的数据匹配的意图,它往往是重要的,要知道除了它的URI中的数据类型(MIME类型)。例如,一个组件能够显示的图像数据不应该被要求播放音频文件。

在许多情况下,可以推断的数据类型的URI -特别 内容: URI的,这表明该数据位于设备和控制内容提供商(见 separate discussion on content providers).)。但类型也可以被显式设置在Intent对象。使用setData()方法指定数据只能作为一个URI,setType() 仅指定一个MIME类型,以及setDataAndType()指定一个URI和MIME类型。getData()方法读取的URI 和类型的getType() 

类别
一个字符串,包含其他信息的组成部分,应该处理的意图。任何类别描述可以被放置在一个Intent对象。因为它不采取行动,意图类定义几个类别,这些常数,包括:常数意CATEGORY_BROWSABLE目标活动,可以安全地通过浏览器调用显示的链接所引用的数据 - 例如,图像或电子邮件。CATEGORY_GADGET可以嵌入到另一个活动的承载小玩意活动。CATEGORY_HOME活动显示在主屏幕上,第一个屏幕设备时开启或首页按钮被按下时,用户可以看到。CATEGORY_LAUNCHER活动可以是任务的初始活性,并在顶层的应用程序启动上市。CATEGORY_PREFERENCE目标活动是一个偏好面板。

看到的意向类的描述类的完整列表。

addCategory()方法放置在一个Intent对象的一个类别,removeCategory()删除以前增加了一个类别,并的GetCategories()得到目前所有类别的集合中的对象。

Extras
键-值对,应处理的意图传递给组件的其他信息。正如一些行动与特定种类的数据URI配对,一些搭配特定的Extras。例如, ACTION_TIMEZONE_CHANGED意图“"time-zone “额外标识新的时区,ACTION_HEADSET_PLUG有一个“ 国家 “额外耳机是否插上或拔下,以及一个”  “的额外耳机类型。如果你发明了一种SHOW_COLOR行动,颜色值将被设置在一个额外的键-值对。

Intent对象有一系列把...()方法用于插入额外的数据和一组类似的各类得到...()方法读取数据。这些方法并行的捆绑对象。事实上,演员可以安装和使用的Bundle读putExtras()getExtras()方法。

标志
各种各样的标志。许多指示Android系统如何开展的活动(例如,活动应该属于哪个任务),以及如何对待它后,它的推出(例如,它是否属于近期活动列表)。所有这些标志定义在意向书类。

Android系统和应用平台来聘用意向对象都发送系统起源广播的激活系统定义的组件。来看看如何构建一个意图激活一个系统组件, 参考咨询 的意向清单。

Intent对象

一个Intent对象是一个捆信息,包含对intent有兴趣的组件的信息(如要执行的动作和要作用的数据)、Android系统有兴趣的信息(如处理intent组件的分类信息和如何启动目标活动的指令)。下面列出它的主要信息:

2.1、组件名字

处理intent的组件的名字。这个字段是一个ComponentName对象——是目标组件的完全限定类名(如"com.example.project.app.FreneticActivity")和应用程序所在的包在清单文件中的名字(如"com.example.project")的组合。其中组件名字中的包部分不必一定和清单文件中的包名一样。

组件名字是可选的,如果设置了,intent对象传递到指定类的实例;如果没有设置,Android使用intent中的其它信息来定位合适的目标组件(见下面的Intent解析)。组件的名字通过setComponent()setClass()setClassName()设置,通过getComponent()读取。

2.2、动作

一个字符串命名的动作将被执行,或在广播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.

查看更多的动作请参考Intent类。其它的动作定义在Android API中,我们还可以定义自己的动作字符串一再我们的应用程序中激活组件。自定义动作字符串应该包含应用程序报名前缀,如"com.example.project.SHOW_COLOR"。

动作很大程度上决定了剩下的intent如何构建,特别是数据(data)和附加(extras)字段,就像一个方法名决定了参数和返回值。正是这个原因,应该尽可能明确指定动作,并紧密关联到其它intent字段。换句话说,应该定义你的组件能够处理的Intent对象的整个协议,而不仅仅是单独地定义一个动作。

一个intent对象的动作通过setAction()方法设置,通过getAction()方法读取。

2.3、数据

数据(data)是将作用于其上的数据的URI和数据的MIME类型。不同的动作有不同的数据规格。例如,如果动作字段是ACTION_EDIT,数据字段将包含将显示用于编辑的文档的URI;如果动作是ACTION_CALL,数据字段将是一个tel:URI和将拨打的号码;如果动作是ACTION_VIEW,数据字段是一个http:URI,接收活动将被调用去下载和显示URI指向的数据。

当匹配一个intent到一个能够处理数据的组件,通常知道数据的类型(它的MIME类型)和它的URI很重要。例如,一个组件能够显示图像数据,不应该被调用去播放一个音频文件。

在许多情况下,数据类型能够从URI中推测,特别是content:URIs,它表示位于设备上的数据且被内容提供者(content provider)控制。但是类型也能够显示地设置,setData()方法指定数据的URI,setType()指定MIME类型,setDataAndType()指定数据的URI和MIME类型。通过getData()读取URI,getType()读取类型。

2.4、种类

此外,还包含关于应该处理intent的组件类型信息。可以在一个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_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 key 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.

更多的种类常量请参考Intent类。

addCategory()方法添加一个种类到Intent对象中,removeCategory()方法删除一个之前添加的种类,getCategories()方法获取Intent对象中的所有种类。

2.5、附加信息

额外的键值对信息应该传递到组件处理intent。就像动作关联的特定种类的数据URIs,也关联到某些特定的附加信息。例如,一个ACTION_TIMEZONE_CHANGE intent有一个"time-zone"的附加信息,标识新的时区,ACTION_HEADSET_PLUG有一个"state"附加信息,标识头部现在是否塞满或未塞满;有一个"name"附加信息,标识头部的类型。如果你自定义了一个SHOW_COLOR动作,颜色值将可以设置在附加的键值对中。

Intent对象有一系列的put…()方法用于插入各种附加数据和一系列的get…()用于读取数据。这些方法与Bundle对象的方法类似,实际上,附加信息可以作为一个Bundle使用putExtras()getExtras()安装和读取。

2.6、标志

有各种各样的标志,许多指示Android系统如何去启动一个活动(例如,活动应该属于那个任务)和启动之后如何对待它(例如,它是否属于最近的活动列表)。所有这些标志都定义在Intent类中。