Android Activity intent-flilter filtering mechanism

Source: Internet
Author: User

an application in Android (APP1) if you want to invoke an activity (Activity2) of another application (APP2), you need to set the parameters in the intent to be declared in the manifest file by Activiy2 <intent-filter> property validation to successfully invoke the activity. The validation that can be used in the <intent-filter> tag is: Action validation, category validation, data validation, where action validation must be declared.

    • Action Validation:

An activity can declare multiple actions, as long as the validation passes through one, if Activity2 declares the following 3 action:

             <intent-filter>                <action android:name= "ACTION1"/>                <action android:name= "ACTION2"/>                <action android:name= "ACTION3"/>, ...             ... </intent-filter>

Other apps can be called to Activity2 by setting the action method to intent (if other apps in the system have the same action, a list is available for the user to choose from), such as:

<span style= "FONT-SIZE:18PX;" >intent intent=new Intent () intent.setaction ("ACTION1"); StartActivity (Intent);</span>



    • Category validation:

Activity2 <intent-filter> can also declare multiple category validations, as follows (where default is automatically added, not declared or will exist):

         <intent-filter>                <action android:name= "ACTION1"/>                <action android:name= "ACTION2"/>                <action android:name= "ACTION3"/>                <category android:name= "Android.intent.category.DEFAULT"/>                <category android:name= "CATEGORY1"/>                <category android:name= "CATEGORY2"/>                <category Android:name= "CATEGORY3"/>            </intent-filter>

In other applications, you can add a category to intent, and when the category you add is declared in Activity2, it is verified by the following intent:

        Intent intent=new Intent ();        Intent.setaction ("ACTION1");        Intent.addcategory ("CATEGORY1");        Intent.addcategory ("CATEGORY3");        StartActivity (Intent);




    • Data validation:

Data validation compares complex points and can verify the MIME type and URI set in intent. The Mime type can specify the type of resource being processed, such as "audio/*" for all audio resources, and set MimeType:

            <intent-filter>                <action android:name= "ACTION1"/>                <action android:name= "ACTION2"/>                <action android:name= "ACTION3"/>                <category android:name= "Android.intent.category.DEFAULT"/>                <data                    android:mimetype= "audio/*"                    />            </intent-filter>

The intent of the call can be set with MIME Type by Settype () or settypeandnormalize (),

        Intent intent=new Intent ();        Intent.setaction ("ACTION1");        Intent.settype ("audio/*");       Intent.settypeandnormalize ("audio/*");        StartActivity (Intent);

URI comparison URL wide range, in the form of: Scheme://host:port/path, where scheme for protocol identification such as: HTTP, host for IP, domain name, port is the port number, path is the route. In <intent-filter> <data> can be set to verify this several, but can not jump settings, such as set the scheme, do not set the host, directly set the port, then the port settings will be ignored, Same as not setting port. In setting path, you can use path to set the entire path, or you can use Pathprefix to set the path to start with, or you can use Pathpattern to set the path with the wildcard character, with the wildcard character "*" and ". *", where "*" and "*" in the regular expression. Indicates that the previous character is 0 or any number, while the "." Represents any character. If "Abc*d" can Match "ABCCCD", "ABCD" and so on, "abc.*d" can Match "abced", "ABCFD" and so on. The <data> settings URI format in <intent-filter> is as follows:

            <intent-filter>                <action android:name= "ACTION1"/>                <category android:name= " Android.intent.category.DEFAULT "/>                <data                    android:scheme=" http "                    android:path="/abc.jsp                    " Android:host= "www.google.com"                    android:port= "8080"                    android:pathprefix= "/in"                    android:pathpattern= "/I . *n "                    />            </intent-filter>

The corresponding call intent can be as follows:

        Intent intent=new Intent ();        Intent.setaction ("ACTION1");        Uri Uri=uri.parse ("http://www.google.com:8080/abc.jsp");//Match path       //URI Uri=uri.parse ("http://www.google.com : 8080/index.jsp ");//Match Pathprefix        //Uri Uri=uri.parse (" Http://www.google.com:8080/idexn ");//Match Pathpattern        Intent.setdata (URI);        StartActivity (Intent);

However, it is important to note that the intent SetData () method and Settype () cover each other, that is, if you set the MIME Type first with Settype (), then the URI is set with SetData (). Then the original set of MimeType will be emptied, and vice versa. The following is the SetData () method source code, read it is easy to understand:

Public Intent SetData (Uri data) {        mdata = data;        Mtype = null;        return this;}

If an activity's <intent-filter> <data> MimeType and URI are set, then you need to use Intent's Setdataandtype (Uridata, String Type) is set.

In summary, if an activity declares action validation, category, and data validation in <intent-filter>, then another application wants to invoke the activity implicitly, You must set the correct parameters to the intent, and only all of them can be called to this activity by verification. It is also important to note that the Exported8 property of the called activity needs to be set to True (the default) to be called by other applications.

















Android Activity intent-flilter filtering mechanism

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.