This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

In everyday Android development, we use IntentFilter matching rules. IntentFilter’s main rules are divided into action, category, and Data categories. Only a perfect match can successfully start the target Activity.

Today we are going to explain;

First, the Activity invocation mode

There are two modes of Activity invocation: explicit invocation and implicit invocation.

1. Explicit call

In most cases, explicit calls are the most common:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);

startActivity(intent);
Copy the code

The component information of the object being launched, including the package name and the class name, is not included in the package name:

Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName cn = new ComponentName("com.test","com.test.MainActivity"); intent.setComponent(cn); startActivity(intent);Copy the code

Implicit call

The Intent is required to match the filter set in the IntentFilter of the target component. The target Activity cannot be started if it does not match;

Intent intent = new Intent(); intent.setAction("android.intent.action.View"); startActivity(intent);
Copy the code

IntentFilter Matching rule description

1. Matching rules for Action

  • Action is a string. The system predefined some actions, but we can also define our own actions in the application.
  • The action in the Intent must match the action in the filter.
  • The content in the action is case sensitive;
  • If no action is specified in the Intent, the match fails.
  • If there are multiple actions in a filtering rule, the Intent can be matched as long as the action in the Intent is the same as any action in the Activity filtering rule.
<activity android:name=".BActivity" > <intent-filter> <action android:name="com.ysl.test"/> <action The android: name = "com. Ysl. Test1" / > / / must add a category android: name = "android. Intent. The category. The DEFAULT" otherwise an error "category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".AActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> btn_skip_b.setOnClickListener { Intent.action = "com.ysl. Test "intent.action = "com.ysl. Test" intent.action = "com.ysl. Test "intent.action = "com.ysl.Copy the code

Common actions are as follows (constants in Intent classes)

  • Intent.action_main identifies the Activity as the start of a program
  • Intent.action_view displays the user’s data
  • Intent.ACTION_DIAL indicates the user dial panel
  • Intent.action_sendto sends a message
  • Intent.ACTION_PICK: Selects information from the list. It is usually used to select contacts or pictures
  • Intent.action_answer: Processes incoming calls
  • Intent.action_chooser displays an Activity selector, such as where common choices are shared

2. Matching rules for category

A category is a string, and the system predefined some categories, and you can also define your own category in your application;

The matching rules for category are:

  • You can have no categories in an Intent, but if there are several categories, each should be able to match any category in the filter;
  • An Intent can have multiple categories, and all categories in the Intent must match the Activity.
  • Also can not set the category, the system will automatically match android. The intent. The category. The DEFAULT;
  • This might feel a lot like an action, but if you look at it a little bit, you’ll notice that the Intent is setAction and addCategory, so there’s only one action. However, an Activity can have multiple actions in its intent-filter, and categories can have multiple categories, all of which must appear in the Activity’s category set.

Note:

  • Since it is mandatory for an Activity to have one, we do not add this categoty to the intent to match it;
  • If you just addCategory alone doesn’t work, you have to setAction it;
<! -- Intent-filter --> <intent-filter> <action Android :name="com.axe. Mg. what" /> <category android:name="com.yu.hu.category1"/> <category android:name="com.yu.hu.category2"/> <category android:name = "android.intent.category.DEFAULT" /> </intent-filter> <! --ThirdActivity intent-filter--> <intent-filter> <action Android :name="com.axe. Mg. what" /> <category Android :name=" intent-filter "-- <intent-filter> <action Android :name="com.axe "android.intent.category.DEFAULT" /> <category android:name="com.yu.hu.category1"/> <category android:name="com.yu.hu.category2"/> <category android:name="com.yu.hu.category3"/> </intent-filter> <! --FourthActivity intent-filter--> <intent-filter> <action Android :name="com.axe. Mg. what" /> <category Android :name= "android.intent.category.DEFAULT" /> <category android:name="com.yu.hu.category2"/> </intent-filter> Intent intent = new  Intent(); intent.addCategory("com.yu.hu.category1"); intent.addCategory("com.yu.hu.category2"); intent.setAction("com.yu.hu.what"); startActivity(intent);Copy the code

3. Matching rules of data

Matching rules for data: The Intent must contain data, and the data must match exactly one of the data specified in the filtering rule.

The syntax format of data

<data android:scheme="string"

android:host="string"

android:port="string"

android:path="string"

android:pathPattern="string"

android:pathPrefix="string"

android:mimeType="string" />
Copy the code

Data consists of two parts: mimeType and URI. URI is in the following format, including scheme, host, port, path, pathPrefix, and pathPattern.

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

Specific parameter explanation:

  • MimeType: refers to the media type, such as image/ JPEG, audio/ MPEG4-generic, and Vidio /. It can refer to different media formats, such as pictures, texts, and videos.
  • Scheme: The mode of the URI, such as HTTP, file, content, etc. If scheme is not specified in the URI, then all other parameters of the URI are invalid, which means that the URI is invalid.
  • Host: the host name of the URI, such as blog.csdn.net. If host is not specified, all other parameters in the URI are invalid, which means that the URI is invalid.
  • Port: the port number in the URI, such as 80. The port parameter is meaningful only when scheme and host parameters are specified in the file URI.
  • Path: indicates the complete information about the path.
  • PathPrefix: indicates the pathPrefix.
  • PathPattern: Complete path information, but it can contain wildcard *, indicating 0 or any characters;

Notes for Data

  • The URI may not be set, but if it is, the scheme and host properties must be set.
  • The scheme attribute of the URI has a default value of content or file. Therefore, even if the URI is not set for data in the intent-filter, the scheme and host attributes need to be set at the same time. The value of the scheme property must be content or File.
<intent-filter>

<action android:name="xx" />

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

<data

android:host="www.baidu.com"

android:pathPrefix="/imgs"

android:port="8080"

android:scheme="https" />

</intent-filter>

Intent intent = new Intent();

intent.setData(Uri.parse("https://www.baidu.com:8080/imgs/img1.png"));

startActivity(intent);
Copy the code

IntentFilter summary

1. IntentFilter Matches the priority

Intent-filter = action->data->category;

2, implicit intent;

Each through startActivity () method of implicit Intent has at least one category, is android. Intent. The category. The DEFAULT, So as long as it is to receive an implicit Intent of the Activity should include android. The Intent. The category. DEFAULTcategory, otherwise will cause the failure of the Intent to match

An activity component that wants to be called by another component with an implicit intent is declared in androiddmanifest.xml as follows:

<activity android:name="com.. test.MainActivity"> <intent-filter> <action android:name="com.test.test" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>Copy the code

conclusion

It is almost the end of the year, we should study hard, can find a good job;