Intent matching in Android: the role of Intentfilter

Source: Internet
Author: User

"0" The basic design concept of Android is to encourage the reduction of coupling between components, so Android provides intent (intent), intent provides a common messaging system that allows you to pass intent between your application and other applications to perform actions and generate events. Use intent to activate three core components of Android applications: activities, services, and broadcast receivers.

The "1" intent can be divided into Cheng Xian intent and implicit intent.

Explicit intent: Calling the Intent.setcomponent () or Intent.setclass () method explicitly specifies the intent of the component name as an explicit intent, explicitly specifying which component intent should pass to.

Implicit intent: Intent that do not explicitly specify the component name are implicit intent. The Android system handles this intent by finding the most appropriate component for the action, category (category), data (URI, and data type) set in the implicit intent.

"2" How does Android find the most appropriate component for implicit intent?

Remember before we defined an activity by specifying a intent-filter,intent filter (intent filter) which is actually used to match implicit Intent, when an intent object is tested by an intent filter, only three aspects are referred to: the action, Data (URI and data type) and category.

1. Action Test

<intent-filter> elements can include child element <action>, such as:

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

A <intent-filter> element should contain at least one <action>, or any intent request cannot match the <intent-filter>. If the intent requested action and a certain <action> match in <intent-filter>, the intent passes this <intent-filter> action test. If a specific action type is not specified in the intent request or <intent-filter>, the following two scenarios occur.

(1) If the <intent-filter> does not contain any action type, then no matter what intent request can not match this <intent-filter>;

(2) Conversely, if the action type is not set in the intent request, the intent request will smoothly pass <intent-filter> if the action type is included in the <intent-filter> Behavior Test.

2. Data test

The data in <intent-filter> is described as follows:

<intent-filter ... >

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

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

</intent-filter>

The <data> element specifies the data URI and data type of the intent request that you want to accept, and the URI is divided into three parts to match: scheme, authority, and path. The URI data type and scheme of the Inteat request set with SetData () must be consistent with the specified in Intentfilter. If a authority or path is also specified in the Intentfilter, they also need to match to pass the test.

The data test compares the URI and data types specified in both the intent object and the filter.

The rules are as follows:

A. An intent object that contains neither a URI nor a data type can pass the test only if the filter also does not specify any URIs and data types.

B. An intent object that contains a URI but no data type can pass the test only if it matches a URI in its URI and a filter that also does not specify the data type. This usually occurs on a URIs similar to mailto: and Tel: They do not refer to actual data.

C. An intent object that contains a data type but does not contain a URI can pass the test only if the filter enumerates the same data types and does not specify a URI.

D. An intent object that includes both a URI and a data type (or a data type that can be inferred from a URI) can be tested if its type matches the type enumerated in the filter. It can also pass the test if its URI matches a URI in this filter or if it has a content contents: or file File:uri and the filter does not specify a URI. In other words, a component is assumed to support content: and file: Data if its filter enumerates only one data type.

3. Category Test

<intent-filter> elements can contain <category> child elements, such as:

<intent-filter ... >

<category android:name= "Android. Intent.Category.DEFAULT "/>

<category android:name= "Android. Intent.Category.BROWSABLE "/>

</intent-filter>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.