Analysis of Android Intent Filter Matching Rules

  • 2021-12-11 08:51:29
  • OfStack

Preface

As we all know, there are two ways to start an activity: one is to show the start, or it is very simple to specify an active class; The other one is implicit startup, which specifies action, category and data information, such as when we start the system camera. Look at the code under 1:


Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
    startActivityForResult(intent,1);

Among them, "android.media.action.IMAGE_CAPTURE" is the action of the camera, so that the camera can be started.
Implicit startup we also use less in normal times, for their own applications of Activity are directly displayed started. When do you use implicit startup? 1 is usually when starting activity of other applications, such as the camera mentioned above.
action, category and data mentioned above are intent-filer, that is, filters to filter activity to be started.
What's the use of intentFiler? It's like tagging yourself. For example, if you label yourself as a college student, then when you say that students come out, it will match you. This is what intentfiler does. Used to filter matches.
So what are these three action, category and data? What are their specific matching rules? As mentioned above, intentFiler is used to start other applications. What common intentfiler can be used? Next, let's take a look.

Structure of intentFilter

As mentioned earlier, there are three intentFilter: action, category and data. Let me look at the code under 1 and get familiar with it under 1:


<intent-filter>
    <action android:name="huan"/>
    <category android:name="android.intent.category.DEFAULT"/>
   </intent-filter>

Also includes what we are most familiar with:


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

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>

These three have different meanings. What kind of activity you want to boot can be booted to the corresponding activity by setting these properties. When we set intentFiler, we should also pay attention to its significance. Although many of them can be set casually, just like the variable name 1, don't start casually.

Action

action is the simplest and most commonly used.

Meaning: This parameter indicates what to do to start this activity. For example, the camera above is android.media.action.IMAGE_CAPTURE, which is obviously the camera function. The essence of action is also a string. To match, each character must be 1 sample, including case. As mentioned above, although you can write this string at will, it should be meaningful. Matching rule: The matching rule of action is also very simple. If action in Intent matches any one action in intentFilter, the matching is successful. But if action in Intent is empty, the match fails.

Category

This parameter is usually used less, and 1 will only be used in 1 special case

Meaning: The common meaning of this parameter is to indicate the class that implements the action action, that is, the component class that can respond to the Intent. For example, the above category android: name= "android. intent. category. LAUNCHER" means that this action will be executed at the top level. What does that mean? It is the first activity that will be opened every time we open the application. Matching rule: Multiple category can be set. However, every category in intent must match one category in intentFilter to match successfully. Note: When setting intentFilter to activity, if there is no other category, category android: name= "android. intent. category. DEFAULT" must be set. The reason is that when startActivity or starActivityForResult are executed, if category is not in intent, android. intent. category will be automatically added.

Data

data is the most complex of the three. As the name implies, this parameter is used to pass data. data differs from the previous two in that it consists of two parts: Uri + mimeType.
Let's first look at the composition of data:


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

data1 consists of 7 parameters. Let's see what they mean:

scheme: This mode representing uri has the most familiar http://This is one mode, and there are two more common ones in Android: content://and file://. Readers who have studied ContentProvider should be familiar with content mode. host, port: host is the host, and port is the port number, which are collectively called authority. For example, www. baidu. com should be very familiar. Which contentProvider is represented in ContentProvider. path, pathPattern, pathPrefix: These three represent path information. 1 is the complete path, 2 can be represented by wildcard characters such as image/*, and 3 is the prefix of the path. mimeType: This represents the media type. E.g. image/jpeg

After talking about his structure, some readers may find that this data is not an address + file type. Yes, uri itself means address. When do we usually use data? For example, we call the camera to take pictures and store them in the specified folder, so how can we let the camera know the address? It is data. We start the camera through intent and transmit the address on data. The uri here also involves the influence of Android version. Interested readers can learn about it.

So, what are the matching rules of data?
And action is a sample, data must be required in intent, and one of intentFilter can be matched successfully.

Note: If uri is not set for data in intentFilter, the default schme is content and file.

Setting intentFilter

After reading the above, I know how to match the three parameters in intentFilter. Do you know how to set intentFilter for activities and how to transmit parameters to intent? This is relatively simple and simple:
Setting intentFilter for activity is relatively simple, as long as it is set in AndroidManifest, see the sample code:


<activity android:name=".MainActivity">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>

It is not difficult to set parameters for intent, one by one:

action: It can be written in when creating a new Intent object, for example: Intent intent = new Intent ("android. intent. action. GET_CONTENT"); Or call the setAction method of Intent: intent. setAction ("android. intent. action. GET_CONTENT"); caterogy: intent. addCategory () by the method of intent; data: This is special because it has two parts: uri and mimeType. There are three methods: setType and setData are set to mimeType and uri, respectively. However, both methods will empty the data of the other one. What do you mean? For example, if I set up an uri through setData and then set up an mimeType through setType, the first uri will disappear and be deleted. So there is a third method: intent. setDataAndType. This method takes two parameters, uri and mimeType, and if you set both parameters at the same time, it will not be cleared.

Commonly used intentFilter

As mentioned above, intentFilter is mainly used to start other applications, such as cameras and telephones, so what are more commonly used? Specific can see this blog android commonly used URI worth remembering. If you don't know, you can leave a message in Baidu or comment area.

Summary

As we mentioned above, intentFilter can be used to filter activity to be started, which is also the same for service and broadcast, and intentFilter can also be set for them to implicitly start the corresponding components. The most common use is to start activities implicitly, especially when calling activities of other applications. It is also important to master some common calls.
At the same time, the matching rules of intentFilter are also very important, so you won't make mistakes when you set intentFilter.
There are still many details that are not clear. Readers who have questions can leave a message in the comment area.

The above is the details of Android Intent-Filer matching rules analysis. For more information about Android Intent-Filer matching rules, please pay attention to other related articles on this site!


Related articles: