An in-depth understanding of intent in Android

Source: Internet
Author: User

(1) Intent provides a generic messaging system that allows intent to perform actions and events in your application, using intent to activate three types of core components of Android apps: activity activities, service services, Broadcast recipient broadcast.

(2) Intent is divided into Hermit's intention and manifestation .

Explicit: Calling the Intent.setcomponent (), Intent.setclassname () or Intent.setclass () method explicitly formulates the component name of the intent for explicit, explicit formulation of which component to activate.

(3) Hermit intent: No explicit designation of the component; Below is a case of a hermit's intentions.

You should first add the appropriate intent-filter to the manifest file

<applicationandroid:icon= "@drawable/ic_launcher"android:label=" @string/app_name " >                        <activityAndroid:label= "@string/app_name"android:name=". Intentactivity " >                                    <intent-filter >                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=". Otheractivity " android:label=" @string/windowtitle ">            <intent-filter >                <action android:name="cn.itcast.zhangxx"/>                <action android:name="Cn.itcast.laoli"/>                <category android:name="Cn.itcast.category.java"/>                 <category android:name="Android.intent.category.DEFAULT"/>                <category android:name="cn.itcast.category.php"/>                <data android:scheme= "itcast" android:host=" www.itcast.cn " android:path="/liming "/>                <data android:mimetype="image/*"/>            </intent-filter>                    </activity>    </Application>

In the Mainactivity.java

 Public void openactivity(View v) {/** * (without data parameters) as long as the action and category in the intent appear in the Intent-filter, you can match it, or the match fails * /Intent Intent =NewIntent ();//implicit intent to activate activityIntent.setaction ("Cn.itcast.zhangxx"); Intent.addcategory ("Cn.itcast.category.java");//Intent.setdata (Uri.parse ("itcast://www.itcast.cn/liming"));        //Intent.settype ("Image/jpeg");        //According to the Android API you can know        //this method automatically clears any data is previously set (for example by SetData (Uri)).        //That is, if you set the SetData method to set the Settype method, then the Settype method will clers out the SetData method        //The solution is as follows:Intent.setdataandtype (Uri.parse ("Itcast://www.itcast.cn/liming"),"Image/jpeg"); StartActivity (Intent);//The Android.intent.category.DEFAULT category is added for intent inside the method}

The following are the rules of the Hermit's intent:

Intent Filter (Intent filter) is actually used to match the implicit Intent, when an intent object is matched by an intent filter, only three aspects are referred to: action, data (URI and data type), and category.

Action Tests (action test)

An Intent object can only specify an action name, and a filter may enumerate multiple action names. If the intent object or filter does not specify any action, the result is as follows:

? If the filter does not specify any action, all intents are blocked, so all intents are tested to fail. There is no intention to pass this filter.

? On the other hand, as long as the filter contains at least one action, an intent object without a specified action automatically passes this test

Category Test

For an intent that can be tested by category matching, the category in the intent object must match the category in the filter. This filter can enumerate another category, but it cannot be omitted from any of the categories in this intent.

In principle an object with no category should always be able to pass the match test, regardless of what is in the filter. In most cases this is the right one. With one exception, Android treats all implicit intentions passed to startactivity () as if they contain at least one category: "Android.intent.category.DEFAULT" (Category_default constant). Therefore, activities that want to receive implicit intent must include "Android.intent.category.DEFAULT" in their intent filters. (Exceptions are filters with "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER" settings)

Data test

When a URI in an intent object is used to compare to a URI in a filter, the various components of the URI are compared. For example, if the filter only specifies a scheme, all the scheme's URIs can match the filter, and if the filter specifies a scheme, hostname but no path, all URIs with the same scheme and hostname can match this filter. Regardless of their path; If the filter specifies a scheme, hostname, and path, only URIs with the same scheme, hostname, and path can match the filter. Of course, a path specification in a filter can contain wildcard characters, so that only a partial match is required.

The data test compares both the intent object and the specified URI and data type in 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 its URI matches a URI in a filter that also does not specify a data type. This usually happens on a URIs like mailto: and Tel: such: 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 lists the same data type and does not specify a URI.

D. An intent object that contains 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. If its URI matches a URI in the filter or if it has a content: or file File:uri and the filter does not specify a URI, it can pass the test as well. In other words, a component is assumed to support content: and file: Data if its filter lists only one data type.

An in-depth understanding of intent in Android

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.