Brief introduction of Intent Filter matching rules in Android

  • 2021-07-01 08:14:23
  • OfStack

This article mainly introduces the rules of implicit Intent matching target components. If there are unclear or inaccurate descriptions, I hope you can point out, thank you:)

1. Introduction to Intent

Intent is used to open another component within one (Component, such as Activity, Service, Broadcast Receiver).

Intent can be divided into implicit (implicitly) and explicit (explicitly):

Explicitly Intent: Used when knowing which specific Component to open, the target Component can be opened by specifying the caller and callee;
Implicitly Intent: If you don't know exactly which Component to turn on, the system will find a 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. Usually used as follows:


Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key", "value");
startActivity(intent);

Executing the above code causes the target Component (in this case MainActivity) to be created (1 series of lifecycle methods such as onCreate are called). In the corresponding lifecycle method in MainAcitivity, the data transmitted from Intent1 can be obtained through getIntent. getXxxExtra ("key").

(2) Implicitly Intent

Implicitly Intent decouples the caller and callee nicely:

The caller describes his Intent through three aspects: action, data and category, and the callee describes what intentions he can respond to through a series of Intent Filter declared in the manifest file. In this way, the caller and callee do not need to know each other, and can work together well through the link between them, Implicitly Intent.

For a more detailed introduction of Intent, you can refer to the official document, which mainly introduces the matching rules of Implicitly Intent.

2. Intent Filter matching rules

Only when action, data and category3 all match, Intent can be regarded as a successful match, and then the corresponding Component can be opened. If a single Component declares multiple Intent Filter, you only need to match any one to start the component.

(1) Matching rules of action

Multiple action can be declared in one Intent Filter, and action in Intent is identical to any one of them in string form (note that it is case sensitive), and action matches successfully. action can be set for Intent by setAction method, or action can be passed in when Intent is constructed. It should be noted that the implicit Intent must specify action. For example, we defined the following Intent Filter for MyActivity in the Manifest file:


<intent-filter>
  <action android:name="android.intent.action.SEND"/>
  <action android:name="android.intent.action.SEND_TO"/>
</intent-filter>

Then as long as the action of Intent is "SEND" or "SEND_TO", this Intent can match the above Activity successfully in terms of action. For example, our Intent is defined as follows:

Intent intent = new Intent("android.intent.action.SEND")
...
Then our Intent matches MyActivity in terms of action.

The Android system predefines many action, and these action represent some common operations. Common action 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 of data

data can be further divided into uri (composed of scheme, host, port, path, pathPattern, pathPrefix) and mimetype. The uri of the Intent can be set by the setData method, and the mimetype can be set by the setType method. Implicit Intent must also specify data. Similar to action, the data aspect matches successfully as long as the data of Intent is identical to any one of data declarations in Intent Filter. It should be noted that if uri is not specified in the data declaration part of Intent Filter, the default uri is content or file, and the scheme part of uri in Intent needs to be content or file to match; To specify the full data for Intent, the setDataAndType method must be used, for reasons shown in the source code for the setData and setType methods:


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

public Intent setType(String type) {
  mData = null;
  mType = type;
  return this;
}

As you can see from the above code, setData sets mimeType to null, and setType sets uri to null. Let's illustrate the matching of data under 1. First, let's look at the syntax for specifying data in 1 Intent Filter:


<data android:scheme="... "  
     android:host="..."
     android:port="..."
     android:path="..."
     android:pathPattern="..."
     android:pathPrefix="..."
     android:mimeType="..." />

scheme, host and other parts do not need to be specified. Suppose we specify the following data for Intent Filter of MyActivity:


<intent-filter>
  <data android:mimeType="vidoe/mpeg" android:scheme="http" android:host="www.xxx.com" />
  <data android:mimeType="text/plain" android:scheme="http" />
</intent-filter>

Then our Intent wants to match, mimeType can be "text/plain" or "video/mpeg", scheme must be "http", and host is not limited, because the second data does not specify host.

(3) Matching rules of category

Unlike action and data, category in Intent must both appear in Intent and Filter to be matched successfully. category may not be specified in Intent. If category is not specified in Intent, the system will automatically bring it "android. intent. category. DEFAULT". Therefore, an Component that wants to receive an Implicitly Intent must carry "android. intent. category. DEFAULT" in the manifest file's Intent Filter declaration. We can add category to Intent using the addCategory method.

(4) Query whether there is an Component that can receive the specified Intent

Adopting resolveActivity of PackageManager or resolveActivity of Intent will obtain an Activity which is most suitable for Intent; queryIntentActivities that calls PackageManager returns all Activity that successfully match Intent. You can refer to the official documents for the detailed definition of these methods, which will not be repeated here.

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: