This article introduces the rules of implicit Intent matching target components. If there are any unclear or inaccurate statements, please point out them.

Intent is used to open another Component within a Component (such as an Activity, Service, or Broadcast Receiver). The former has the distinction of “implicit” and “explicitly” :

  • Explicitly Intent: used when it is known which specific Component is to be opened. The target Component can be opened by specifying the caller and the called.
  • Implicitly Intent: Without knowing exactly which Component to open, the system will find the matching Component by indicating action, data, and category.

Explicitly Intent

When you know exactly which Component you want to open, it’s your cup of tea. Usually used like this:

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

Executing the above causes the target Component (in this case MainActivity) to be created (onCreate(…)). Wait for a series of lifecycle methods to be called). Getinten.getxxxextra (” key “) is used in the corresponding lifecycle method in MainAcitivity to get the data passed along with the Intent.

Implicitly Intent

Implicitly Intent does a good job of decoupling the caller and the called: The caller describes his Intent through action, data, and category, and the caller describes which intents he can respond to through a series of Intent filters declared in the manifest file. Callers and callers can work together with Implicitly Intent, a link that connects them, without having to know each other.

For more information about intents, see the official documentation and related blogs for more information about the matching rules for INTents.

Intent Filter Matches a rule

The Intent matches only if the action, data, and category all match. Then the corresponding Component can be opened. If a Component declares multiple Intent filters, it only needs to match any of them to launch the Component. *

Action matching rule

Multiple actions can be declared in an Intent Filter. If any of the actions in an Intent are identical in the form of a string (note that they are case sensitive), the action will match. An action can be set for an Intent using the setAction method, or passed in when an Intent is constructed. Note that an implicit Intent must specify an action (or data or mimeType if no action is specified). In this case, the IntentFilter will match as long as it contains at least one action. For example, in our Manifest file we define the following Intent Filter for MyActivity:

 
   
  
Copy the code

As long as the Intent action is “SEND” or “SEND_TO”, the Intent matches the Activity in terms of action. For example, our Intent is defined as follows:

Intent intent = new Intent("android.intent.action.SEND");Copy the code

So our Intent matches MyActivity in terms of action. The Android system has a number of predefined actions that represent common actions. Common actions (constants in Intent classes) are as follows:

Intent.ACTION_VIEW
Intent.ACTION_DIAL
Intent.ACTION_SENDTO
Intent.ACTION_SEND
Intent.ACTION_WEB_SEARCHCopy the code
Data matching rules

Data can be further divided into uri (by the scheme, host, port, path | pathPattern | pathPrefix this 4 parts) and mimetype. The URI of the Intent can be set using the setData method, and the mimeType can be set using the setType method. An implicit Intent must also specify data. Similar to an action, as long as the Intent’s data is identical to any of the data declarations in the Intent Filter, the data aspect matches. Note that: If the data declaration part of an 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 specify the full data for an Intent, you must use the setDataAndType method.

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

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

As you can see from the above code, setData sets mimeType to null and setType sets URI to null. Let’s take an example of data matching. First, let’s look at the syntax of the Intent Filter that specifies data:

Copy the code

You do not need to specify scheme and host. Suppose we specify the following data for MyActivity’s Intent Filter:

 
   
  
Copy the code

So our Intent wants to match. MimeType can be “text/plain” or” video/mpeg”,scheme must be “HTTP “, and host is unlimited because the second data does not specify a host.

Matching rules for categories

Unlike action and data, each category in an Intent must appear in the Intent Filter for a match to succeed. Intent may not specify a category, if the Intent is not specified in the category, system will automatically bring for it “android. Intent. The category. The DEFAULT”. So, want to receive Implicitly Intent Component must be in the manifest file Intent Filter statement on “android. Intent. The category. The DEFAULT”. We can add a category to an Intent using the addCategory method.

Query for any Component that can receive the specified Intent

Using the PackageManager’s resolveActivity or Intent’s resolveActivity method will get the Activity that best fits the Intent. Calling the PackageManager queryIntentActivities returns all activities that successfully matched the Intent. For detailed definitions of these methods, you can refer to the official documentation, which is not repeated here.

1. Android Development Art Quest

2.Android Docs