PS: This article is a reprint of the article, read the original source code can be obtained, at the end of the article there are links to the original

In Android development, there are two ways to start an Activity. There are two ways: display start and implicit start. Display start is to specify the information of the component object (package name and class name). Implicit launch is the Activity IntentFilter in the AndroidManifest.xml file; If an Activity’s article startup display shows startup and is implicitly started, then it starts as startup. If the Intent does not match the IntentFilter of the target Activity, the Intent will not match the IntentFilter of the target Activity. If the Intent does not match the IntentFilter of the target Activity, the Intent will not match the IntentFilter. Then starting the Activity will fail.

An Activity can have more than one IntentFilter. An Intent that matches any one IntentFilter can successfully launch the Activity. Only an Intent that matches any action (at least one), category (at least one), or data (at least one) of the IntentFilter matches successfully starts the Activity.

Here’s a simple IntentFilter configuration that I wrote myself:

<activity android:name=”.SecondActivity”>

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

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="homePage"
                android:scheme="recyclemobilephones" />
            <data android:mimeType="video/*"/>
        </intent-filter>
    </activity>

1. Matching principle of action

An IntentFilter can have multiple actions. In the Intent, the value of an action is the same as that of any action in the IntentFilter, including the case of the action. In the Intent, the value of an action must be a string. It only counts as a successful match between them.

2. The matching principle of categories

Category is a string type, the value in the system defines several categories, we can also customize several categories; An Intent may or may not have more than one class. If the Intent has more than one class, then at least one of the same class as the IntentFilter is required to be a match. If there is no class in the Intent, then the IntentFilter must be configured

< category android: name = “android. Intent. The category. The DEFAULT” / >, otherwise matching failure, because in the Activity of startActivity method implementation

Add a category in the DEFAULT android. Intent. The category, the DEFAULT.

3. Matching principle of data

Data is made up of several different attributes whose values are strings. If the data is defined in the IntentFilter, then the Intent adds the matching data to the switch. The following are the properties of the state data:

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

Let’s analyze the data. Generally speaking, the data is composed of mimeType and URI. Mimetype refers to various media formats such as image/JPEG, video/*, etc. The data format of URI is more complex, and there are also URIs:

<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]

The false schema is not in the URI, so the other parameters of the URI are bound.

Host: The host name, such as www.xxx.com, XXX is usually the name of the company. If the host is not defined, then the other parameters of the URI are also excellent, just like the schema.

Port: The port number, such as 8080.

PATH: The full path information.

PathPattern: Complete path information, which can also contain wildcard *.

PathPrefix: The location information for the path.

Note that if the Intent is to use the entire data, namely the URI and the mimeType, you can call the setDataAndType method of the Intent. You cannot call the setType and setData method together. Both methods empty each other’s values. One of the two methods must be empty. See the source code for both methods.

Intent setType method:

public @NonNull Intent setType(@Nullable String type) {

    mData = null;
    mType = type;
    return this;

}

Intent’s setData method:

public @NonNull Intent setData(@Nullable Uri data) {

    mData = data;
    mType = null;
    return this;

}

OK, here’s an example of implicitly starting an activity. For each example, the InENTFilter must add a line of code like this:

<category android:name=”android.intent.category.DEFAULT” />

(1) Use the predefined action of the system to start the Activity:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(Intent.ACTION_VIEW); Intent. PutExtra (“key”,” this is the system’s predefined action to launch the Activity”) startActivity(Intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

     <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />

</intent-filter>

(2) Start the Activity with a custom action:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(“xiaoer”); Intent. PutExtra (“key”,” this is a custom action to start the Activity”) startActivity(Intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

      <action android:name="xiaoer" />
       <category android:name="android.intent.category.DEFAULT" />

</intent-filter>

(3) the use of system Activity, predefined category start action with android. Intent. Action. The VIEW:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(Intent.ACTION_VIEW); Intent.addCategory (intent.category_app_browser) intent.putExtra(“key”,” this is a pre-defined category to launch the Activity”) startActivity(intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.APP_BROWSER"/>
   <category android:name="android.intent.category.DEFAULT" />

</intent-filter>

(4) the use of custom category start the Activity, the action with android. The intent. The action. The VIEW:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(Intent.ACTION_VIEW); Intent.addCategory (“xiaoer_study”) intent.putExtra(“key”,” this is a custom category to launch the Activity”) startActivity(Intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

    <action android:name="android.intent.action.VIEW" />
    <category android:name="xiaoer_study" />
    <category android:name="android.intent.category.DEFAULT" />

</intent-filter>

(5) using only contains the uri of the data to start the Activity, the action with android. The intent. The action. The VIEW:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(Intent.ACTION_VIEW); Intent. SetData (Uri. Parse (” recyclemobilephones: / / homePage “)) intent. PutExtra (” key “, “this is to use only contains the Uri of the data start Activity”) startActivity(intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <data
        android:host="homePage"
        android:scheme="recyclemobilephones" />

</intent-filter>

(6) using a uri and mimeType data to start the Activity, the action with android. The intent. The action. The VIEW:

The code for the Intent that executes the startActivity method is as follows

var intent: Intent = Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(“recyclemobilephones://homePage_2″),”text/plain”) Intent. PutExtra (” Key “,” This is to start the Activity with the data containing the URI and mimeType “) startActivity(Intent)

The IntentFilter configuration for the Activity in the AndroidManifest.xml file is as follows

<intent-filter>

   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <data
        android:host="homePage_2"
        android:mimeType="text/plain"
        android:scheme="recyclemobilephones" />

</intent-filter>

The interface after running the APP is as follows:

The picture

Click any of the please buttons. If the selection box pops up, click the “InintFilter” APP

The picture