天天看點

Intents and Intent Filters---Intent Objects

[b]Intent Objects[/b]

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對象中主要包含以下資訊:

[b]一、Component name[/b]

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

[b]二、Action[/b]

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常量,例如:】

[img]http://dl.iteye.com/upload/attachment/302257/427e714c-22d1-3369-9a62-95e6608a3cd0.png[/img]

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

【翻譯:[b]action屬性[/b]會很大程度決定intent對象的其他資訊的構成,尤其是[b]data屬性[/b]和[b]extras屬性[/b],這就如同方法名決定了方法的參數以及是否有傳回值是一個道理,該屬性的設定和讀取可以用setAction() 和 getAction()方法執行。】

[b]三、Data[/b]

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.

【翻譯:[b]data屬性[/b]由兩部分内容構成:[b]資料URI[/b]和[b]資料的MIME類型[/b],不同的[b]action屬性[/b]往往決定了[b]data屬性[/b]。比如,如果說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所關聯的[b]資料的MIME類型[/b]。】

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對象中明确設定[b]資料的MIME類型[/b],setData() 方法設定資料的MIME類型,setType() 方法設定 MIME type, setDataAndType()方法可以同時設定這兩個屬性。】

[b]四、Category[/b]

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: 翻譯:[b]種類屬性[/b]用來設定目标元件的類别資訊,起着對[b]action屬性[/b]的補充說明作用。一個intent中可以設定多個種類資訊,和action屬性一樣,系統也在intent類中預定義了幾個種類常量:

[img]http://dl.iteye.com/upload/attachment/302259/a2d24a46-976f-3f27-a0a5-cbdca53b8da7.png[/img]

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執行個體中所有種類屬性資訊。】

[b]五、Extras[/b]

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. 【翻譯:[b]Extras資訊[/b]是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執行個體。】

[b]六、Flags[/b]

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. 【翻譯:[b]Flags屬性[/b]的種類比較多。多數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.