我们知道当我们通过Intent隐式调用组件时,Android通过Action,Category、Data组合定位组件,本节我们以Activity的调用为例,讲解Intent过滤机制。
首先我们明确两个概念:
1 调用者
调用者就是通过Intent调用Activity的一端,调用者需要通过相关的方法设置Action、Category、Data,也就是调用者要设置调用条件。
2 被调用者
被调用者就是声明被调用的窗体,被调用者通过在AndroidManifest.xml文件中的intent-filter标签设置过滤条件。
下面我们学习过滤规则
1)Action验证
对于隐式调用,Action是必须要的,调用者只可以设置一个Action,被调用者可以指定一个或多个Action,如果有一个Action与调用者指定的Action相符,则认为满足条件。如下代码所示。
被调用者配置
<intent-filter> <action android:name="com.salary.pratise.ACTION1"/> <action android:name="com.salary.pratise.ACTION2"/> <action android:name="com.salary.pratise.ACTION3"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> |
调用者代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION2"); startActivity(actionIntent); |
2)Category验证
Category的验证规则如下:调用者为Intent指定了N个Category,被调用者配置了M个Category,只有当N个Category是M个Category的子集时,验证通过。如下代码所示。
被调用者配置
<intent-filter> <action android:name="com.salary.pratise.ACTION4" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.salary.pratise.CATEGORY1" /> <category android:name="com.salary.pratise.CATEGORY2" /> <category android:name="com.salary.pratise.CATEGORY3" /> </intent-filter> |
调用者代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION4"); actionIntent.addCategory("com.salary.pratise.CATEGORY2"); actionIntent.addCategory("com.salary.pratise.CATEGORY1"); startActivity(actionIntent); return true; |
对于Category有一点需要说明,Categrory也是必填项,这是因为Android系统会默认为调用者添加android.intent.category.DEFAULT,所以自然也需要在被调用者的Intent-filter中添加如下标签:
<category android:name="android.intent.category.DEFAULT" /> |
但是也有个例外,例如
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> |
也就是说,当Intent-filter中只有android.intent.action.MAIN和android.intent.category.LAUNCHER成对出现时,可以不添加android.intent.category.DEFAULT,否则被调用者必须声明android.intent.category.DEFAULT,例如下面代码。
被调用者配置
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.salary.pratise.CATEGORY4" /> </intent-filter> |
调用者代码
Intent actionIntent = new Intent(); actionIntent.setAction("android.intent.action.MAIN"); actionIntent.addCategory("android.intent.category.LAUNCHER"); actionIntent.addCategory("com.salary.pratise.CATEGORY4"); startActivity(actionIntent); |
3)Data验证
Data验证相对比较复杂。我们知道Data由Uri和Mime Type组成。Mime Type指定可以处理的资源类型。我们可以只通过Mime Type设置过滤条件。代码如下:
被调用者配置
<intent-filter> <action android:name="com.salary.pratise.ACTION4" /> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="custom/*"></data> </intent-filter> |
调用者代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION4"); actionIntent.setDataAndType(null, "custom/*"); startActivity(actionIntent); |
注意在指定Mime Type可以使用通配符*表示任意字符。
Uri由四个部分组成,如下所示:
scheme:://host:port/path |
其中
scheme:协议标识
host:主机名
port:端口
path:路径
这里需要说明的是,
1)scheme必须设置,不论是调用者还是被调用者,例如下面代码:
被调用者配置
<intent-filter> <action android:name="com.salary.pratise.ACTION5" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="cust" /> </intent-filter> |
调用者代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION5"); actionIntent.setData(Uri.parse("cust://userserver:80/viewuser/001")); startActivity(actionIntent); |
2)如果被调用者同时指定了Mime Type和Uri,则调用者也必须同时设置Mime Type和Uri代码如下所示:
被调用者配置
<intent-filter> <action android:name="com.salary.pratise.ACTION5" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="custom/*" android:host="userserver" android:scheme="cust" android:path="/viewuser/001"/> </intent-filter> |
调用者代码:
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION5"); actionIntent.setDataAndType(Uri.parse("cust://userserver:80/viewuser/002"), "custom/*"); startActivity(actionIntent); |
如果被调用者只是指定了mimeType,则调用者不能设置Data。
3)Uri的设置不能跳跃设置,例如,当没有指定host时,path属性的过滤是无效的。例如下面代码
被调用者配置
<<intent-filter> |
这段配置是无效的,我们要么添加Host声明,要么删除path声明
这里还需要注意的是端口设置是个例外,可以默认不设置,也不会影响后面的过滤。
4)如果被调用者打算忽略host,可以设置host为通配符*,例如下面代码
被调用者的设置
<intent-filter> <action android:name="com.salary.pratise.ACTION5" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="custom/*" android:scheme="cust" android:host="*" android:path="/viewuser/001"/> </intent-filter> |
这时path属性的验证依然有效
5)除了path属性,还可以设置pathPattern,pathPattern属性可以设置通配符,其中.表示任意字符,*表示出现0或者任意次数。例如下面代码
被调用者的设置
<intent-filter> <action android:name="com.salary.pratise.ACTION5" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="custom/*" android:scheme="cust" android:host="*" android:pathPattern="/viewuser/a.*b"/> </intent-filter> |
调用者的代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION5"); actionIntent.setDataAndType(Uri.parse("cust://userserver/viewuser/a001b"), "custom/111"); startActivity(actionIntent); |
5)除了path属性,还可以设置pathPrefix,表示路径的前缀,所有已pahPrefix开头的路径都满足,设置pathPrefix时必须以/开头。例如下面代码
被调用者的设置
<intent-filter> <action android:name="com.salary.pratise.ACTION5" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="custom/*" android:scheme="cust" android:host="*" android:pathPrefix="/view/u" android:pathPattern="a.*b"/> </intent-filter> |
调用者的代码
Intent actionIntent = new Intent(); actionIntent.setAction("com.salary.pratise.ACTION5"); actionIntent.setDataAndType(Uri.parse("cust://userserver/view/user/a001b"), "custom/111"); startActivity(actionIntent); |
6)当同时设置了path,pathPrefix,pathPattern时,只要有一个满足即可,也就是说他们这些过滤条件是“或”的关系。
7)Uri是大小写敏感的。
大家注意,Intent-Filter机制比较多,尤其是Data相关的过滤机制(所幸太复杂的Data过滤机制使用场景也不多),所以在实际项目,大家应该多做测试,看过滤规则的设置是否满足实际的需要。
后面再讲解Activity、Service、Broadcaster以及系统调用的时候,还会涉及到Intent-Filter,到时候具体问题我们在具体分析。
(张伟:2018年9月27日)
(转载时请注明来源)