Introduction to intent filter matching rules in Android

This paper mainly introduces the rules of implicit intent matching target components. If there is any unclear or inaccurate description, I hope you can point it out. Thank you:)

1. Introduction to intent

Intent is used to open another component in one component (such as activity, service, broadcast receiver).

Intent can be divided into implicit and explicit:

Explicit intent: used when you know which specific component to open. You can open the target component by specifying the caller and callee; Implicitly intent: if you don't know exactly which component to open, the system will find the matching component by pointing out action, data and category. (1)Explicitly Intent

When you know exactly which component you want to open, it is your dish. It is usually used as follows:

Executing the above code will cause the target component (here is mainactivity) to be created (a series of life cycle methods such as oncreate are called). In the corresponding life cycle method in mainactivity, you can get the data transmitted with intent through getintent.getxxextra ("key").

(2)Implicitly Intent

Implicitly intent realizes the decoupling between the caller and the callee:

The caller describes his intent through action, data and category, and the callee describes what intentions he can respond to through a series of intent filters declared in the manifest file. In this way, the caller and the callee do not need to know each other. They can work together through the implicit intent link.

For a more detailed introduction to intent, you can refer to the official documents. Here we mainly introduce the matching rules of implicitly intent.

2. Intent filter matching rules

Only when the action, data and category are matched, the intent is matched successfully, and then the corresponding component can be opened. If a component declares multiple intent filters, it only needs to match any one to start the component.

(1) Matching rules for action

Multiple actions can be declared in an intent filter. The actions in the intent are exactly the same as any one of them in the form of string (note that they are case sensitive), and the actions match successfully. You can set the action for the intent through the setaction method, or you can pass in the action when constructing the intent. Note that the implicit intent must specify action. For example, we defined the following intent filter for myactivity in the manifest file:

As long as the action of the intent is "send" or "send_to", the intent can successfully match the above activity in action. For example, our intent is defined as follows:

Intent = new intent ("Android. Intent. Action. Send")... Then our intent matches myactivity in action.

The Android system predefines many actions, which represent some common operations. Common actions are as follows (constants in the intent class):

Intent.ACTION_ VIEW Intent.ACTION_ DIAL Intent.ACTION_ SENDTO Intent.ACTION_ SEND Intent.ACTION_ WEB_ SEARCH

(2) Matching rules for data

Data can be further divided into URI (composed of scheme, host, port, path | pathpattern | pathprefix) and mimeType. The URI of intent can be set through the SetData method, and the mimeType can be set through the setType method. Implicit intent must also specify data. Similar to action, as long as the data of intent is exactly the same as any data declaration in intent filter, the data aspect will match successfully. Note: if the data declaration part of the intent filter does not specify a URI, the default URI is content or file, and the scheme part of the URI in the intent must be content or file to match; To specify complete data for intent, you must use the setdataandtype method. For the reason, see the source code of SetData and setType methods:

As can be seen from the above code, SetData will set the mimeType to null, and setType will set the URI to null. Let's give an example to illustrate the matching of data. First, let's take a look at the syntax of specifying data in intent filter:

There is no need to specify all parts such as scheme and host. Suppose we specify the following data for the intent filter of myactivity:

If our intent wants to match, the mimeType can be "text / plain" or "video / MPEG", the scheme must be "HTTP", and there is no limit on the host, because the second data does not specify the host.

(3) Matching rules for category

Unlike action and data, the category in intent must both appear in intent filter before matching is successful. Intent can not specify category. If category is not specified in intent, the system will automatically bring "Android. Intent. Category. Default" for it. Therefore, components that want to receive implicitly intent must be marked with "Android. Intent. Category. Default" in the intent filter declaration in the manifest file. We can add category for intent through the addcategory method.

(4) Query whether there is a component that can receive the specified intent

Using the resolveactivity method of packagemanager or the resolveactivity method of intent will obtain an activity that is most suitable for intent; Calling queryintensities of packagemanager will return all activities that successfully match intent. For the detailed definitions of these methods, you can refer to the official documents, which will not be repeated here.

The above is the whole content of this article. I hope it will be helpful to your study.

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
分享
二维码
< <上一篇
下一篇>>