Detailed analysis of IntentFilter matching rules

Detailed analysis of IntentFilter matching rules

[[430933]]

Preface

In daily Android development, we will use the matching rules of IntentFilter. The main rules of IntentFilter are divided into three categories: action, category, and data. Only a perfect match can successfully start the target Activity;

Today we will explain;

1. Activity calling mode

There are two calling modes of Activity: explicit calling and implicit calling;

1. Explicit call

In most cases, we are most often exposed to explicit calls:

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

Strictly speaking, this is not an explicit call, because in the meaning of an explicit call, it is necessary to clearly specify the component information of the object being started, including the package name and class name, but the package name is not specified here:

  1. Intent intent = new Intent(Intent.ACTION_MAIN);
  2. intent.addCategory(Intent.CATEGORY_LAUNCHER);
  3. ComponentName cn = new ComponentName( "com.test" , "com.test.MainActivity" );
  4. intent.setComponent(cn);
  5. startActivity(intent);

2. Implicit call

The Intent needs to match the filter information set in the IntentFilter of the target component. If it does not match, the target Activity cannot be started;

  1. Intent intent = new Intent();
  2. intent.setAction( "android.intent.action.View" );
  3. startActivity(intent);

2. Detailed explanation of IntentFilter matching rules

1. Action matching rules

  • Action is a string. The system predefines some actions, and we can also define our own actions in the application;
  • The matching rule is that the action in the Intent must match the action in the filter rule, which means that the string value of the action is exactly the same;
  • The content in action is case sensitive;
  • If no action is specified in the Intent, it is considered a match failure;
  • If there are multiple actions in a filter rule, the match will be successful as long as the action in the Intent is the same as any action in the Activity filter rule;

  1. <activity android: name = ".BActivity" >
  2. <intent-filter>
  3. < action android: name = "com.ysl.test" />
  4. < action android: name = "com.ysl.test1" />
  5. //You must add category android: name = "android.intent.category.DEFAULT" otherwise an error will be reported
  6. <category android: name = "android.intent.category.DEFAULT" />
  7. </intent-filter>
  8. </activity>
  9. <activity android: name = ".AActivity" >
  10. <intent-filter>
  11. < action android: name = "android.intent.action.MAIN" />
  12. <category android: name = "android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. btn_skip_b.setOnClickListener {
  16. //Click the button in A to start B
  17. var intent = Intent()
  18. intent.action = "com.ysl.test"  
  19. startActivity(intent)
  20. }

Common actions are as follows (constants in the Intent class)

  • Intent.ACTION_MAIN, identifies the Activity as the start of a program
  • Intent.ACTION_VIEW, displays the user's data
  • Intent.ACTION_DIAL, user dial panel
  • Intent.ACTION_SENDTO, send a message
  • Intent.ACTION_PICK, select information from the list, generally used to select contacts or pictures, etc.
  • Intent.ACTION_ANSWER, handles incoming calls
  • Intent.ACTION_CHOOSER, displays an Activity selector, such as the common choice of where to share

2. Category matching rules

Category is a string. The system predefines some categories, and we can also define our own categories in the application;

The matching rules for category are:

  • There can be no category in the Intent, but if there is a category, no matter how many there are, each one must be able to match any category in the filter rule;
  • An Intent can have multiple categories, and all categories in the Intent must match the Activity;
  • You can also not set the category, in which case the system will automatically match android.intent.category.DEFAULT;
  • This may feel very similar to action, but if you pay a little attention, you can find that Intent is setAction and addCategory, that is, there is only one action (note that an Intent has only one action, but an Activity's intent-filter can have multiple actions), while there can be many categories and all categories must appear in the Activity's category set;

Notice:

  • Because it is mandatory that an Activity needs a , so we don't need to add this category to the intent to match;
  • If addCategory is used alone, it is useless, it must be done after setAction;

  1. <! --SecondActivity's intent-filter-->  
  2. <intent-filter>
  3. < action android: name = "com.axe.mg.what" />
  4. <category android: name = "com.yu.hu.category1" />
  5. <category android: name = "com.yu.hu.category2" />
  6. <category android: name = "android.intent.category.DEFAULT" />
  7. </intent-filter>
  8. <! --ThirdActivity's intent-filter-->  
  9. <intent-filter>
  10. < action android: name = "com.axe.mg.what" />
  11. <category android: name = "android.intent.category.DEFAULT" />
  12. <category android: name = "com.yu.hu.category1" />
  13. <category android: name = "com.yu.hu.category2" />
  14. <category android: name = "com.yu.hu.category3" />
  15. </intent-filter>
  16. <! --FourthActivity's intent-filter-->  
  17. <intent-filter>
  18. < action android: name = "com.axe.mg.what" />
  19. <category android: name = "android.intent.category.DEFAULT" />
  20. <category android: name = "com.yu.hu.category2" />
  21. </intent-filter>
  22. Intent intent = new Intent();
  23. intent.addCategory( "com.yu.hu.category1" );
  24. intent.addCategory( "com.yu.hu.category2" );
  25. intent.setAction( "com.yu.hu.what" );
  26. startActivity(intent);

3. Data matching rules

Data matching rule: The Intent must contain data, and the data must completely match a data in the filtering rule;

The syntax of data

  1. <data android:scheme= "string"  
  2. android:host= "string"  
  3. android:port= "string"  
  4. android:path= "string"  
  5. android:pathPattern= "string"  
  6. android:pathPrefix= "string"  
  7. android:mimeType= "string" />

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

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

Specific parameter explanation:

  • mimeType: refers to the media type, such as image/jpeg, audio/mpeg4-generic, vidio/, etc., which can represent different media formats such as pictures, texts, and videos;
  • Scheme: The mode of the URI, such as http, file, content, etc. If the scheme is not specified in the URI, the other parameters of the entire URI are invalid, which also means that the URI is invalid;
  • host: the host name of the URI, such as blog.csdn.net. If the host is not specified, the other parameters in the entire URI are invalid, which also means that the URI is invalid;
  • port: The port number in the URI, such as 80. The port parameter is only meaningful when the scheme and host parameters are specified in the input URI;
  • path: the complete information of the path;
  • pathPrefix: describes the prefix information of the path;
  • pathPattern: It represents the complete information of the path, but it can contain wildcards *, which means 0 or any characters;

Notes on data

  • The URI may not be set, but if it is set, the scheme and host attributes must be set;
  • The scheme attribute of URI has a default value, which is 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 when matching, and the value of the scheme attribute must be content or file.

  1. <intent-filter>
  2. < action android: name = "xx" />
  3. <category android: name = "android.intent.category.DEFAULT" />
  4. <data
  5. android:host= "www.baidu.com"  
  6. android:pathPrefix= "/imgs"  
  7. android:port= "8080"  
  8. android:scheme= "https" />
  9. </intent-filter>
  10. Intent intent = new Intent();
  11. intent.setData(Uri.parse( "https://www.baidu.com:8080/imgs/img1.png" ));
  12. startActivity(intent);

3. Summary of IntentFilter

1. IntentFilter matching priority

Check the intent-filter and search according to the following priority relationship: action->data->category;

2. Implicit intent;

Every implicit Intent sent by the startActivity() method has at least one category, android.intent.category.DEFAULT, so any Activity that wants to receive an implicit Intent should include android.intent.category.DEFAULTcategory, otherwise the Intent matching will fail.

If an activity component wants to be called by other components through implicit intent, its declaration in AndroiddManifest.xml is as follows:

  1. <activity android: name = "com..test.MainActivity" >
  2. <intent-filter>
  3. < action android: name = "com.test.test" />
  4. <category android: name = "android.intent.category.DEFAULT" />
  5. </intent-filter>
  6. </activity>

Summarize

It's almost the end of the year, everyone should study hard so that they can find a good job;

This article is reproduced from the WeChat public account "Android Development Programming"

<<:  The interface is much cleaner! Alipay is testing the function of closing the homepage column card

>>:  WeChat 8.0.16 is officially updated! Official support for "opening secondary accounts" and six new features have been discovered

Recommend

High-conversion information flow account building routine, just use it directly!

Recently, a friend left a message to complain: Th...

Case analysis: How to build a user incentive system?

The construction of a user incentive system is ge...

Are tall people more likely to have lumbar disc herniation?

Rumor: "Tall people have a heavy burden on t...

Sony Z2 one-day experience

For those of us who are into appearance, we all ju...

QQ 8.5 for iPhone officially released: Split-screen file viewing and new moods

Although it is the weekend, Tencent QQ team relea...

How to place massive Qianchuan search ads!

Qianchuan has fully opened up the delivery of sea...

Practical Tips丨How to conduct a successful fission campaign?

The article starts with the wool party and discus...

Nokia returns to the mobile phone market: Can it really stage a comeback?

Recently, after Nokia spent a lot of money to acq...

Huawei App Market search keyword promotion!

1. Introduction to search keyword promotion As on...

Breathing in liquid, from the deep sea to space

380 million years ago, fish began to crawl onto l...

Analysis on promotion of Migu Reading and Kindle check-in activities!

Some time ago, Migu Reading launched the "Ch...

For content-based APP, how to build a content operation framework?

Where does the content come from? This is probabl...