IntentFilter匹配规则
学习android开发的人大多数都是从先接触Activity开始的,因为它可以让我们比较直观的接触到,用Activity就避免不了会接触Activity的启动方式:显式调用和隐式调用,其中显式调用需要明确指定被启动对象的组件信息,包括包名和类名,而隐式调用则不需要明确指定组件信息,原则上Intent不能就是显式调用又是隐式调用,如果二者共存的话以显示调用为主。
一、显式调用
对于显式调用来说要简单的多,直接上代码
//从MainActivity切换到FirstActivity
Intent intent1 = new Intent(this, FirstActivity.class);
startActivity(intent1);
上面两行代码就完成了最简单的Activity显式调用。
二、隐式调用
重点来了,我们这次要说的重点不在于显式调用,而是下面要说的隐式调用。
隐式调用需要Intent能够匹配目标组件的IntentFilter中所设置的过滤信息,若果不匹配将无法启动目标Activity,IntentFilter中的过滤信息有三个:action、category、data。
1、匹配规则
- 一个过滤列表中可以有多个action,category,data并各自构成不同类别,一个Intent必须同时匹配action类别,category类别和data类别才算完全匹配;
- 一个Activity中可以有多组intent-filter,一个Intent只要匹配任何一组intent-filter就算匹配成功;
下面先看一个过滤规则的例子:
<activity
android:name=".FirstActivity"
android:label="@string/title_activity_first" >
<intent-filter>
<action android:name="com.ax.action.a"/>
<action android:name="com.ax.action.b"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.ax.category.a"/>
<category android:name="com.ax.category.b"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
上面这段是AndroidManifest.xml中的代码,作用是声明一个Activity并为其定义过滤规则。
2、action匹配规则
Intent中的action必须存在且必须和过滤规则中的其中一个action相同。 action其实就是字符串(区分大小写),可以是系统预定义的一些action,也可以是自定义的action,匹配就是指action的字符串值完全一样,每个Intent中 只能有一个 action,而一个过滤规则中可以有多个action,如果intent不指定action,但是category和data能匹配某个activity的过滤规则也能隐式调用该activity(已经经过验证,因为有资料上说不指定action会匹配失败,所以特意验证了下);如果intent指定的action和过滤规则中的某一个action相同则匹配成功。
3、category的匹配规则
category和action的匹配规则不同,它要求Intent中的每个category必须和过滤规则中的一个相同。 也就是说每个Intent中 可以有多个 category,只要Intent中有category,对于每个category都必须能在过滤规则中找到相同category,如果intent中不指定category则会匹配过滤规则中的android.intent.category.DEFAULT,如果有则匹配成功,没有则匹配失败。
4、data的匹配规则
data相比前面两个要复杂些,data的语法如下(两种方式是等效的)
<intent-filter>
<data
android:scheme=""
android:host=""
android:port=""
android:path=""
android:pathPattern=""
android:pathPrefix=""
android:mimeType="" />
</intent-filter>
或者
<intent-filter>
<data android:scheme=""/>
<data android:host=""/>
<data android:port=""/>
<data android:path=""/>
<data android:pathPattern=""/>
<data android:pathPrefix=""/>
<data android:mimeType=""/>
</intent-filter>
data由两部分组成,mimeType和URI。mineType指媒体类型,比如image/jpeg、audio/mpeg4-generic和video/*等,可以表示图片、文本、视频等不同的媒体格式;URI的结构如下:
<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]
为了帮助大家理解,下面举两个例子:
content://com.example.project:200/folder/subfolder/etc
http://www.baidu.com:80/search/info
下面解释下URI中各个位置的含义:
- scheme:URI的模式,比如http、file、content等,如果URI中没有指定scheme,那么整个URI的其他参数无效,这也意味着URI是无效的。
- host:URI的主机名,比如www.baidu.com,如果host未指定,那么整个URI中的其他参数无效,这也意味着URI是无效的。
- port:URI中的端口号,比如80,仅当URI中指定了scheme和host参数的时候port参数才是有意义的。
- path、pathPattern、pathPrefix:这三个参数表述路径信息,其中path表示完整路径信息;pathPattern也表示完整的路径信息,但是它里面可以包含通配符“*”,“*”表示0个或多个任意字符,需要注意的是,由于正则表达式的规范,如果想表示真实的字符串,那么“*”要写成“\\\*”,“\”要写成“\\\\”;pathPrefix表示路径的前缀信息。
过滤规则中可以没有指定URI,但是系统会赋予其默认值:content和file
在了解完data的数据格式以后,我们继续说data的匹配规则,data的匹配规则和action类似,它要求Intent中必须含有data数据,并且data数据能完全匹配过滤规则中的某一个data。
有一点需要注意的地方:如果要为Intent指定完整的data,必须调用setDataAndType方法,不能调用setData再调用setType,因为这两个方法会相互清除对方的值,看setType的源码:
public Intent setType(String type) {
mData = null;
mType = type;
return this;
}
setType会把mData置为null,同理setData也会把mType置为null。
IntentFilter的匹配规则都了解以后我们写一下开始给的intent-filter的完全匹配:
//1
Intent intent2 = new Intent();
intent2.setAction("com.ax.action.a");
intent2.addCategory("com.ax.category.b");
intent2.setDataAndType(Uri.parse("file://abc"), "text/plain");
startActivity(intent2);
//2
Intent intent2 = new Intent();
//intent2.setAction("com.ax.action.a");
intent2.addCategory("com.ax.category.b");
intent2.setDataAndType(Uri.parse("file://abc"), "text/plain");
startActivity(intent2);
//3
Intent intent2 = new Intent();
intent2.setAction("com.ax.action.a");
//intent2.addCategory("com.ax.category.b");
intent2.setDataAndType(Uri.parse("file://abc"), "text/plain");
startActivity(intent2);
以上三种方式均能实现隐式调用。
5、补充
当找不到匹配的Activity时程序会报错,为了避免一些不必要的错误,当我们通过隐式启动一个Activity时,可以判断下有没有能够匹配的Activity,可以采用PackageManager的resolveActivity方法或者Intent的resolveActivity方法判断,如果找不到匹配的Activity就会返回null,代码如下:
Intent intent2 = new Intent();
intent2.setAction("com.ax.action.a");
intent2.addCategory("com.ax.category.b");
intent2.setDataAndType(Uri.parse("file://abc"), "text/plain");
PackageManager pm = getPackageManager();
if (pm.resolveActivity(intent2, PackageManager.MATCH_DEFAULT_ONLY) != null) {
startActivity(intent2);
}else {
Toast.makeText(this, "匹配失败", Toast.LENGTH_SHORT).show();
}
/*if(intent2.resolveActivity(pm) != null)
startActivity(intent2);
else {
Toast.makeText(this, "匹配失败", Toast.LENGTH_SHORT).show();
}*/