Android开发经验谈Android开发

IntentFilter匹配

2018-08-31  本文已影响23人  waiwaaa

Intent不应该同时存在显示调用及隐示调用,同时存在时以显示调用为准 。隐式调用需要Intent能够匹配目标组件的IntentFilter中所设置的过滤信息,如果不匹配将无法启动目标组件。

IntentFilter的过滤信息有:action, category, data 。

匹配规则:

各属性的匹配规则:

action的匹配规则

category的匹配规则

data的匹配规则

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

data由mimeType 和 URI 两部分组成。

mimeType指媒体类型,如image/jpeg、audio/meeg4-generic和video/*等
URI结构:
<sheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]

URI如果没有指定,schema默认值为content和file。
为Intent指定完整的data,必须调用setDataAndType方法,不能调用setData后再试用setType,因为两个方法会相互清空对方的值。

其它

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
PackageManager pManager = this.getPackageManager();
List<ResolveInfo> list=pManager.queryIntentActivities(intent,flags);
if(list==null){
//没有找到匹配的
}

ResolveInfo rf=pManager.resolveActivity(intent,flags);
if(rf==null){
//没有找到匹配的
}

flags指定MATCH_DEFAULT_ONLY这个标志位,仅匹配intent-filter中声明了<category android:name="android.intent.category.DEFAULT"/>这个category的Activity。用此参数,如果查出的不为空,一定成功。

上一篇下一篇

猜你喜欢

热点阅读