Android Intentfilter matching rules

Source: Internet
Author: User

1 Overview

Activity start-up is divided into implicit and explicit two, to use the implicit start, then Intentfilter is a very important point of knowledge.

The matching rules for Intentfilter are explained here.

2 Basic Forms
<activityandroid:name="com.tencent.tauth.AuthActivity"Android:launchmode ="Singletask"android:nohistory="true"android:taskaffinity=" Com.coohuaclient ">                    <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:scheme="tencent1101354300"/>    </intent-filter></activity>

As you can see, there are action,category in Intentfilter, as well as data in several forms of information.

3 Action matching rules

You can configure multiple action values in the Intent-filter in XML, in an implicitly-launched intent, you can specify at most one action value, and you must specify the value of this action if you want to successfully start it implicitly. And this action value must match any one of the values on the XML configuration.

It is important to note that the action is case-sensitive.

4 category matching rules

In an implicitly-launched intent, the system will default to the category Android.intent.category.DEFAULT, so you must specify this value in XML, which is a precondition for a successful invocation. The category rules and action are not the same. It is divided into 2 kinds of cases;

The intent contains category:
Then these category must all match the value specified on the XML.

there is no category in intent:
In this case, the system will set the default category in intent, which is Android.intent.category.DEFAULT, if the value matches the one specified in the XML, it can be called successfully.

5 Data matching rules

Data is relatively complex, so let's take a look at its full data entry:

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

As you can see, data is made up of two parts, part of which is mimetype represents the media type of the data, and the other is the URI, which specifies the location of the resource.

Look at the structure of the URI:

://:/[||]
For example:
Content://com.baidu.haha:8080/dir/src/text.txt
Http://www.baidu.com:80/dir/src/text.txt

The two examples above illustrate the format pairs of URIs.

What is worth explaining here is the difference between Path,pathprefix and Pathpattern, where path refers to the complete path, for example in the case of an appeal, where path stands for "/dir/src/text.txt", and Pathprefix represents the starting part of the full path, can be/dir or/dir/src,pathpattern is a path that conforms to a regular expression, and a regular expression can be used to represent the path, thus matching is achieved. It is important to note that, in Pathpattern, if you want to express a character such as this kind of function in the regular expression, then you need to escape, but it is not possible to write "*", because the manifest file is an XML file, he is read into memory by the system, it will be escaped once, The Pathpattern is escaped once when it is read as a regular expression, so there are two escapes. Therefore,the "\" should be written, and "\" is written as "\ \"

The following two ways of writing the data are consistent:

<intent-filter ;  Span class= "Hljs-tag" ><data  android:scheme  = "file"  android:host  =  "www.baidu.com" /> </ intent-filter ;  < Intent-filter ;  <data  android:scheme  =/>  <data  android:host  = "www.baidu.com" />  </ intent-filter ;  
6 Precautions

When using implicit startup, it's a good idea to determine if there is an activity that matches the implicit intent, or the startup will go wrong. To determine whether there are qualifying activity there are two methods, one is to use the Resolveactivity method of Packagemanager, one is to use the intent resolveactivity, if the match is not found to return null, We can determine if there is a suitable activity based on the return value.

Look at the method:

public ActivityInfo resolveActivityInfo(PackageManager pm, int flags)

The flags here need to be explained, and we will generally use the MATCH_DEFAULT_ONLY flag, because this flag bit represents the Only the activity that declares the Android.intent.category.DEFAULT in XML is returned, which is the precondition for the implicit start success.

Android Intentfilter matching rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.