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

Intents and Intent Filters---Intent Objects

阅读更多
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对象是一个包含请求信息的对象,包含可能被激活的目标组件所关心的信息比如action、目标组件可能要操作的数据以及目标组件运行环境(Android系统)所关心的信息(如目标组件的类别描述信息和如何发射一个目标组件即activity的指令性信息)。一个intent对象中主要包含以下信息:

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

    翻译:该属性是响应intent的组件的名称,是ComponentName对象,用来设定响应intent对象的目标组件的完整类名,例如:"com.example.project.app.FreneticActivity",准确说该属性将包含两种信息:类名+包名,在Android应用的manifest文件中包名信息与这个属性中的包名信息不是说必须要匹配的。需要说明的是该属性不是必须指定的属性,如果被指定的话,该intent对象将送交给指定名称的组件的一个实例,如果没有指定,Android系统将根据当前 intent对象中的其他信息来判定哪个组件是最为适合的目标组件,组件名信息的设置和取得可以通过以下方法执行。

    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:【翻译:这个属性是一个字符串属性,用来描述要求目标组件实例执行的操作,Intent 类中预定义了若干action常量,例如:】




    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的类描述文档中预定义了很多可以直接使用的action常量,在Android API的其他地方也有预定义的action常量,开发者也可以自定义action常量(把你的 应用的包名作为自定义action常量的前缀),例如:"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().

    【翻译:action属性会很大程度决定intent对象的其他信息的构成,尤其是data属性extras属性,这就如同方法名决定了方法的参数以及是否有返回值是一个道理,该属性的设定和读取可以用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.

    【翻译:data属性由两部分内容构成:数据URI数据的MIME类型,不同的action属性往往决定了data属性。比如,如果说action属性被设定为ACTION_EDIT,那么data属性就可能是:将要显示并允许用户编辑的数据的URI;如果action属性被设定为ACTION_CALL,那么data属性就应该是tel: URI和要呼叫的电话号码;如果action属性被设定为ACTION_VIEW,data属性的URI是http:,那么目标activity将根据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.

    【翻译:当系统根据intent对象找到与之相匹配的组件来处理数据的时候,系统除了需要知道数据的URI(数据存储的URI)以外,还必须知道要的数据类型,也就是URI所关联的数据的MIME类型。】

    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中推导出来,尤其是URI=content:的时候,这时候数据通常是位于本设备上而且是由某个content provider来控制的。即使如此,还是可以在intent对象中明确设定数据的MIME类型,setData() 方法设定数据的MIME类型,setType() 方法设定 MIME type, setDataAndType()方法可以同时设定这两个属性。】

四、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: 翻译:种类属性用来设定目标组件的类别信息,起着对action属性的补充说明作用。一个intent中可以设定多个种类信息,和action属性一样,系统也在intent类中预定义了几个种类常量:



   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. 【翻译:使用addCategory() 方法可以添加一个种类属性信息,使用removeCategory() 方法可以删除一个种类属性信息;可以用getCategories()方法取得当前intent实例中所有种类属性信息。】

五、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. 【翻译:Extras信息是Key-value形式的附加信息,存放在intent中用来传递给某个组件,可以理解为方法调用时的参数。】

    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...()的方法,可以用来向intent对象插入各种类型的Extras信息。同样也有着方法名如同get...()的方法可以取得各种类型的Extras信息。使用putExtras() 和 getExtras()方法同样可以设定或读取Extras信息,这时候两个方法的参数(putExtras())或返回值(getExtras())是Bundle实例。】

六、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. 【翻译:Flags属性的种类比较多。多数Flags属性是为了告知Android系统如何发射某个目标activity组件的,比如目标activity组件实例是属于当前task还是新建的一个task;如果当前栈顶恰好是该目标组件实例的话,系统如何处理。】

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.
  • 大小: 28 KB
  • 大小: 22.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics