Detailed explanation of matching rules of intentfilter in Android

Matching rules for intentfilter

Use of intentfilter

If we want to start an activity implicitly, we need to successfully match any intent filter in the activity. To successfully match the intent filter, we need to completely match the action, category and data in the intent filter.

Example:

If the filter list of the activity is configured as above, we need to meet the requirements of action, category and data at the same time when setting the parameters of intent to calculate that the matching is successful, and the activity can be started successfully only if it is completely matched. (if it is a broadcastreceiver, you only need to meet either action or data type.).

Matching rules for action

Action is a string. The system predefines some actions. At the same time, we can also define our own actions. The matching rule of action is that the action in intent must match the action in the filtering rule (the string value is exactly the same and case sensitive). There can be multiple actions in a filter rule, so as long as the action in intent can be the same as any action in the filter rule, it can be matched successfully. In addition, if an action is defined in the filter rule, there must also be a matching action in intent.

Matching rules for category

The matching rule of category is that if category is included in intent, all categories must be the same as one of the categories in the filter rule. In other words, if a category appears in intent, it must be the same as a category in the filter rule. If there is no category, the intent can still be matched successfully (this is different from action). As for why you can match without setting category? The reason is that the system will add android.intent.category.default by default when calling startactivity or startactivityforresult. At the same time, in order to accept implicit calls, our intent filter in androidmanifest.xml should also specify the category of "Android. Intent. Category. Default".

Matching rules for data

The matching rule of data is similar to that of action. If data is defined in the filtering rule, matching data must also be defined in intent. At the same time, as long as the action in intent can be the same as any action in the filtering rule, it can be matched successfully

Syntax of data

Data consists of two parts, mimeType and URL. MimeType refers to media types, such as image / jpeg, audio / mpeg4 generic and video / *. The URL contains more data. The following is the structure of the URL:

For example:

In addition, it should be noted that if the URL is not set in the configuration list in androidmanifest.xml, only the mimeType is set, for example:

URLs are actually URLs with default values, and the default values are content and file. That is, if the setting in Android manifest.xml is the same as above. We need this to match:

summary

To sum up, if you want to start an activity implicitly.

1. View the matching list of activities in Android manifest. Select an intent filter to match. If action or data is defined, our intent must also contain action or data that can match it.

2. Create intent and set action or data. Category can be set or not (if all settings are to match).

3. Ensure that the set action, data and category match correctly.

4. Call the method to start the activity: startactivity or startactivityforresult.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>