天天看點

Intents and Intent Filters--Intent filters

[b]Intent filters[/b]

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.

翻譯:三類核心元件為了使得系統知曉哪些隐式intent對象可以被自己所響應,通常聲明一個或多個intent過濾器,過濾器的作用是聲明目前元件的能力,表示目前元件所能接受的intent對象。需要注意的是如果是顯式intent,intent過濾器是不起任何作用的,這時候顯式intent直接發送給目标元件執行個體(當然你可以使用extras和flags來傳遞必要的資料和發送相關的系統指令)。而對于隐式intent,隻有當它通過某個 intent過濾器三個測試項的檢測後才可以被發送到某個元件執行個體。

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

翻譯:一個元件可以執行不同類型的操作,在manifest檔案中的元件元素的定義中,針對不同的操作可以分别多個intent過濾器子元素,例如在該系列文檔中提供的Note Pad樣例應用中的NoteEditor activity,就定義了兩個過濾器,一個過濾器針對使用者查閱或編輯Note的操作,另外一個過濾器針對使用者建立Note的操作,換句話說表示該 activity可以執行兩種不同類型的操作。

[table]

|[b]Filters and security[/b]

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.【翻譯:intent過濾器不能用于安全控制管理,intent過濾器的作用是為某個隐式intent打開一個元件,它無法阻止顯式intent直接啟動目标元件。】

[/table]

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

翻譯: intent filter是IntentFilter類的執行個體,然而,由于Android系統在它啟動某個元件執行個體之前,就必須知道這個元件能做什麼操作,是以intent過濾器的建立是不會使用Java代碼來建立的的,而是在應用的manifest檔案中直接定義intentFilter元素的。(這裡有一個例外:直接通過Context.registerReceiver()方法動态注冊的廣播接收元件的過濾器是直接建立過濾器執行個體的。)

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對等的三類資訊([b]action, data, category[/b]),一個隐式intent要能夠激活某個元件的前提是:必須通過某個過濾器這三項([b]action, data, category[/b])比對測試,

Each of the three tests is described in detail below:

[b]一、Action test[/b]

An <intent-filter> element in the manifest file lists actions as <action> subelements. For example【翻譯:manifest檔案中的intent過濾器元素中可以如下定義action子元素:】:

[quote]<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>[/quote]

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.【翻譯:上例中的過濾器至少應該有一個action,否則該過濾器将阻塞所有隐式intent。】

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要想通過某個intent過濾器的action測試,該intent執行個體的action必須能夠和目前intent過濾器中的某個action比對。如果intent執行個體或是intent過濾器中沒有指定action的話,将會發生以下測試結果】:

1、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.【翻譯:如果是intent過濾器中沒有指定action,那麼比對肯定失敗,導緻該過濾器将阻塞所有隐式intent。】

2、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執行個體中沒有指定action的話,隻要該過濾器聲明了至少一個action的話,該intent執行個體将通過目前action測試。】

[b]二、Category test[/b]

An <intent-filter> element also lists categories as subelements. For example:【翻譯:manifest檔案中的intent過濾器元素中可以如下定義category子元素:】

[quote] <intent-filter . . . >

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

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

. . .

</intent-filter>[/quote]

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.【翻譯:注意:在manifest檔案中,過濾器元素中定義的action、category子元素不使用Android API中的常量名,而是直接使用常量值。】

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執行個體要想通過類别測試的話,在目前這個intent執行個體中的每個類别資訊都必須和目前的intent過濾器中的類别清單中的值比對上,也就是說目前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()方法傳遞的隐式intent都會自動添加一個 android.intent.category.DEFAULT 類别屬性資訊,是以對于intent過濾器來說常常必須定義一個android.intent.category.DEFAULT 類别屬性資訊。對于那些定義了android.intent.action.MAIN 和 android.intent.category.LAUNCHER 類别的intent過濾器來說不是因為這個原因而在intent過濾器中定義了android.intent.category.LAUNCHER類别屬性, 标記有這兩個類别資訊的intent過濾器有特殊的含義:要求目前intent過濾器所關聯的元件(Activity)在裝置發射屏以新task的方式被立即激活(按下menu鍵後),而且這時候包含android.intent.category.LAUNCHER 類别不是必須的。

[b]三、Data test[/b]

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:

[quote]<intent-filter . . . >

<data android:mimeType="video/mpeg" android:scheme="http" . . . />

<data android:mimeType="audio/mpeg" android:scheme="http" . . . />

. . .

</intent-filter>[/quote]

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和data類型組成,其中URI是由:[b]schema、host、Port、path[/b]四部分組成的。

scheme://host:port/path

For example, in the following 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.

翻譯:上例中的[b]scheme[/b]="content", [b]host [/b]="com.example.project", [b]port [/b]="200", [b]path[/b]= "folder/subfolder/etc", URI中的host和Port組成了URI的authority,如果host未指定的話那麼Port即使指定了也被系統忽略。

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.

翻譯:URI中的各個組成部分是可選的,但是又是關聯的,如果要使得URI的[b]authority[/b]有意義,必須先指定[b]scheme[/b],要使得[b]path[/b]有意義,又必須先指定URI的[b]authority[/b] 。

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.

翻譯:data測試中的URI測試通過的原則是:隻要目前intent過濾器中列舉出的URI部分與目前intent中的URI資訊可以比對即通過Uri測試。需要注意的是在intent過濾器的URI中的path定義中可以使用通配符

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.

翻譯:注意 在intent過濾器的data 類型定義中可以使用通配符

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:

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

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

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

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

翻譯:data 測試基本原則如下:

1. 如果一個Intent對象中既不包含URI也不包含data type,這時候如果目前intent過濾器同樣也沒有指定URI和data type的時候,data測試通過。

2. 如果一個Intent對象中包含URI但是不包含date type(而且資料類型無法從URI中推斷出),這時候如果目前intent過濾器同樣隻是指定了相應的URI的場合,data測試通過。以上場合僅僅适用于 URIs 是 mailto: 和 tel: 無法推斷實際資料類型的時候。

3. 如果一個Intent對象中包含date type但是不包含URI,這時候如果目前intent過濾器同樣隻是例舉了相應的date type的場合,data測試通過。

4. 如果一個Intent對象中同時包含URI和date type(或資料類型可以從URI中推斷出),這時候如果目前intent過濾器列舉了相應的date type的場合,data type部分的測試通過。如果目前intent過濾器也列舉了相應的URI,則通過URI部分的測試。這時候如果說目前intent指定的data URI是content:或 file:的時候,即使目前過濾器沒有指定URI,也通過URI部分的測試。換句話說,當元件過濾器中僅僅指定了data類型而沒有明确指定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. 【翻譯:如果一個intent通過一個以上的activity或service的過濾器測試的話,這時候系統将讓使用者選擇哪個元件來響應該intent請求,如果沒有找到目标元件來響應目前intent的話,将發生異常。】

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【翻譯:上面說到的第四個原則使得元件能夠從檔案或内容提供器中讀取本地資料,是以,作為過濾器可以僅僅例舉資料類型、不需要明确指定content: 和file:schemes,這是很典型的,像下面的<data>元素中就是這樣做的,它告知Android系統該元件可以從内容提供器中獲得圖像資料,然後顯示相關的圖像資料。】:

[quote]<data android:mimeType="image/*" />[/quote]

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:【翻譯:另外一個很常見的配置過濾器的方式是使用scheme 和資料類型來配置過濾器,例如下面的例子,告知Android系統該元件可以通過網絡取得視訊資料并顯示該視訊資料。】

[quote]

<data android:scheme="http" android:type="video/*" />[/quote]

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.【翻譯:想一下:當使用者根據一個連結要顯現相關的畫面資料,這時候浏覽器應用程式首先做的是顯示資料,如果無法顯示的話,将會建立一個隐式intent,其中包含了資料類型和schema等資訊,然後請求一個activity來做資料顯示工作,顯示資料需要下載下傳管理器先下載下傳資料,然後把下載下傳的資料交給某個内容提供器控制。。。】

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:【翻譯:多數應用也有另外一種途徑來啟動更新(無需對任何資料的引用),activity執行個體可以設定它自己的action屬性="android.intent.action.MAIN",如果這個執行個體需要在發射台上呈現出來的話,可以設定它的category屬性="android.intent.category.LAUNCHER"】

[quote]<intent-filter . . . >

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

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

</intent-filter>[/quote]