Android intent filter-judge whether intent matches the object

Source: Internet
Author: User
Tags home screen
Android intent filter-judge whether intent matches the object

I. Intent receiving Principle
When the user sends an intent, the component is required to perform the operation. If this intent has clearly set the componentname, the intent will be directly sent to the specified component and triggered. If no settings are set, android system automatically determines which component the intent should be sent.

Most of the operations that can be handled by components in their own case, intent usually directly specifies which component to be processed, if no element name is specified, the intent is mostly used to trigger components on other applications.

Ii. Functions of intent Filter
In the Android system, how does one determine which intent the component can receive depends on the intent filter settings added to the manifest release when declaring the activity or service, each activity can be set to 0 ~ Multiple sets of intent filters, each set of intent filters is a comparison rule

When an intent is generated, the system will check the intent filter in each component in the manifest, and the dynamic component will be checked. If the filter is not set, only intent with clearly specified component can be received.

Iii. Intent Filter
The intent filter contains three types of information: Action, data, and category. That is to say, the filter will only compare with the three items in the intent, and each filter can have multiple data actions and category at the same time.

Although each intent filter is different from the intentfilter type, the android system must modify its compatibility because of the fact that the component price is dynamic, therefore, intent filters are all stored in androidmanifest in XML format. XML contents, instead of Java code. The only exception is that the broadcast receivers intent filter can be set through context. registerreceiver ().
Example

 
1 Context. registerreceiver (broadcastreceiver receiver, intentfilter filter)

Add the filters to the dynamic filter during the upload.

Values to be set in intent filter, including action, data, and category, are defined in intent class, that is to say, in the intent object, setting the value of these three types of data and setting the value of these three types of data in the intent filter are all defined in the intent class, in Java code, the number of variables referenced by class can be passed. However, to set the value in manifest, you need to check the string value of the actual number of variables in class.

For example, set filter in manifest. xml.

 1      <  Intent  - Filter...  >  
2 < CATEGORY Android: Name = " Android. Intent. Category. Default " />
3 < CATEGORY Android: Name = " Android. Intent. Category. browsable " />
4 ...
5 </ Intent - Filter >

Set intent object category by javacode

 
1 Intent. setcategory (intent. category_default );

4. Intent test find the component to receive intent
When intent is generated, it compares with the intent filter defined by the component in manifest. When the comparison is made, three tests are required. When all three tests pass, to confirm that this component can handle this intent. If more than one activity or service passes the test at the same time, the system will ask which activity you want to activate. If none of them meet the test requirements, an issue will occur.

5. Intent test-action test

 
1. if no action is specified for an intent object, at least one intent action must be contained in pass2.filter. If no action exists, no intent can pass the filter.

Example

 1      <  Intent -  Filter...  >  
2 < Action Android: Name = " Com. example. Project. show_current " />
3 < Action Android: Name = " Com. example. Project. show_recent " />
4 ...
5 </ Intent - Filter >

6. Intent test-category Test

 
1. if the intent has a set category, the intent object must be able to pass through all the categories set by the filter before test can pass. If the intent has not set a category, set pass3. when the specified row startactivity () is used, at least one category: intent will be used when no preset intent is specified. category_default (that is, "android. intent. category. default ")

Therefore, if the activity needs to receive the intent, the intent filter part of the activity must have the category of "android. intent. category. the default value can be received, that is, the activity in manifest must have at least intent. category_defaul (that is, "android. intent. category. default "), and set intent in the activity that has been set as an animated activity. category_defaul

Set the category value in the filter, and set the complete string value. In this part, check the intent class because the values accepted by the XML tag are strings, setting the intent category in the Java code can use the class's constant number to calculate the category.

VII. Intent test-data test
Each data tag contains the URI and data type (MIME media type)
Uri is represented by scheme, host, port, and path.
That is, you can regard a complete URI

 
Scheme: // host: Port/path

If the host is not specified, the port can be omitted.
Data Type can use * to indicate the information that allows any type of metadata.
For example, text/* "or" audio /*"
Example
Uri is

 1   Content:  //  Com. example. Project: 200/folder/subfolder/etc  
2  
3 Set the filter condition method
4
5 < Intent - Filter... >
6 < Data Android: mimetype = " Video/MPEG " Android: Scheme = " Content " Android: Host = " Com. example. Project " Android: Port = " 200 " Android: Path = " Folder/subfolder/etc " />
7 < Data Android: mimetype = " Audio/MPEG " Android: Scheme = " HTTP " ... />
8 ...
9 </ Intent - Filter >

The principle for comparing data in the filter to the data in the intent is

1. when no data tag is set in the filter, and no URI or data type is set in the intent, the test passes through 2. when the filter has a set Uri, no data type is set, and the intent object only has a URI with no data type. At this time, this usually occurs only in mailto: tel: time 3. when only data type is set in the filter, no URI is set, and intent object has the same data type but no URI is passed 4. when the data type and Uri set in the filter are the same as those set in the intent object, that is, if only the data type is set in the filter but the URI is not set, only when the data type in the intent object is matched, The URI is content: or file :.

8. Example
1. Set the element to display any image scheme on the local side.

 
1 <Data Android: mimetype="Image /*" />

When the local contentprovider needs to demonstrate the scheme, you do not need to specify the URI part with content: or file:
2. Set any video clips that can be played from the Internet

 
1 <Data Android: Scheme="HTTP"Android: Type="Video /*" />

When a user needs to open a resource based on the web page, the user will first use the HTML page to open the page. If not, the user will
In the same way as the intent, to find which activity is merged to open this resource. If you do not know, the download manager will open
3. Set an activity for the specified row

 
 1  <  Intent  -  Filter...  >  
2 < Action Android: Name = " Code Android. Intent. Action. Main " />
3 < CATEGORY Android: Name = " Code Android. Intent. Category. Launcher " />
4   </ Intent - Filter >

If you do not need to parse any intent, the activity will be automatically triggered when the app is opened.

9. Intent and intent Filter
Intent filter is used to find the components that intent wants to activate. It also includes the related information of the device when the preset element appears.
For example, Set
"Android. Intent. Action. Main" and "android. Intent. Category. launcher"
This component will appear on the device's top-most launch Checklist

If the filter of the component sets Android. Intent. Category. Home"
The component is displayed on the home screen.

Use the related method identifier of packagemanager class to find the combined element for the specified intent.
Through query... () method, you can find the component that can receive the specified intent.

Through resolve... (), you can find the most suitable component that receives the specified intent.
For example, queryintentactivities () can get the list of all merged activities.

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.