Android Activity intent-flilter filtering mechanism

Source: Internet
Author: User

Android Activity intent-flilter filtering mechanism

In Android, if one app (APP1) wants to call an Activity (Activity2) of another app (APP2 ), the parameters set for Intent can be declared in the configuration file via Activiy2 Property verification can be successfully called to the Activity. Tags can be used for verification: Action verification, Category verification, and Data verification. Action verification must be declared.

Action Verification:

One Activity can declare multiple actions. If one Activity passes verification, it passes verification. If Activity2 declares the following three actions:


Other applications can call Activity2 by setting the Action Method for Intent (if other applications in the system also have the same Action, the list will be provided for the user to choose from), such:

Intent intent=new Intent();intent.setAction("ACTION1");startActivity(intent);

Category Verification:

Activity2 You can also declare multiple Category verifications, as shown below (the DEFAULT one is automatically added by the system and will still exist if it is not declared in time ):


In other applications, you can add a Category for the Intent. When all the added Category is declared in Activity2, it passes verification. The following Intent can pass Verification:

        Intent intent=new Intent();        intent.setAction("ACTION1");        intent.addCategory("CATEGORY1");        intent.addCategory("CATEGORY3");        startActivity(intent);

Data Verification:

Data verification is complex. You can verify the Mime Type and Uri set in Intent. Mime Type can specify the Type of the processed resource. For example, "audio/*" indicates all audio resources. Set MimeType:


The called Intent can set the Mime Type through setType () or setTypeAndNormalize,

        Intent intent=new Intent();        intent.setAction("ACTION1");        intent.setType("audio/*");       // intent.setTypeAndNormalize("audio/*");        startActivity(intent);

Uri has a wide range of URLs in the format of scheme: // host: port/path. scheme indicates the Protocol identity, such as http, host indicates the IP address, domain name, and port indicates the port number, path is the path. In Of For example, if scheme is set, host is not set, and port is directly set, the port setting will be ignored, it is the same as port not set. When setting the path, you can use the path to set the entire path, or you can use pathPrefix to set the path starting with, or you can use pathPattern to set the path with the wildcard "*" and ". * ", where" * "is the same as" * "in the regular expression, indicating that the first character is 0 or any number, and". "represents any character. If "abc * d" matches "abcccd" and "abcd", "abc. * d" matches "abced" and "abcfd. In In Set the Uri format as follows:


The corresponding Intent call can be as follows:

Intent intent = new Intent (); intent. setAction ("ACTION1"); Uri uri = Uri. parse (" 8080/abc. jsp "); // match path // Uri uri = Uri. parse (" 8080/index. jsp "); // match pathPrefix // Uri uri = Uri. parse (" 8080/idexn"); // match pathPattern intent. setData (uri); startActivity (intent );

However, it should be noted that the setData () method and setType () method of Intent will overwrite each other. That is to say, if setType () is used to set the Mime Type, then setData () is used () if Uri is set, the previously set MimeType will be cleared, and vice versa. The following is the source code of the setData () method, which is easy to understand:

public Intent setData(Uri data) {        mData = data;        mType = null;        return this;}

If an Activity's Of If both mimeType and Uri are set, you need to use the setDataAndType (Uridata, String type) of Intent to set it.

To sum up, if an Activity Declare Action verification, Category, and Data verification. To call this Activity implicitly, another application must set the correct parameters for Intent. This Activity can be called only when all parameters are verified. Note that the exported8 attribute of the called Activity must be set to true (default) to be called by other applications.

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