IntentFilter的匹配规则

一.Activity的启动方式

(1).显示调用,要明确的指定被启动对象的组件信息,包括包名类名

(2).隐式调用,需要Intent能够匹配目标组件的IntentFilter所设置的过滤信息,过滤信息有action,category,data。为了匹配过滤列表,需要同时过滤列表中的action,category,data信息,否则失败,一个Intent只要能匹配任何一组intent-filter即可成功启动Activity。

过滤规则示例

①. action的匹配规则

action是一个字符串,系统预定义了一些action,同时我们也可以在应用中定义自己的action,Intent中的action必须能够和过滤规则中的任何一个action相同,action区分大小写。

②. category的匹配规则

category是一个字符串,系统预定义了一些category,同时我们也可以在应用中定义自己的category,与action的区别是,如果Intent中含有category,那么所有的category都必须和过滤规则中的其中一个category相同。如果Intent中不含有category,其实系统默认会为Intent添加上“android.intent.category.DEFAULT”。同时为了我们的activity能够接收隐式调用,就必须在intent-filter中指定<categoryandroid:name="android.intent.category.DEFAULT"/>。

③. data的匹配规则

类似于action,如果过滤条件中定义了data,那么Intent中必须也要定义可匹配的data,

data由两部分组成mimeType和URI.URI的结构如下

<scheme>://<host>:<port>/[<path>|<pathPattern>|<pathPrefix>]

content://com.example.demo:80/file/image/name

http://www.baidu.com:80/search/android

data的语法如下:

<data android:scheme="string" URI模式,如http,file,content

          android:host="string" URI模式,如www.baidu.com

          android:port="string" URI模式,端口号

          android:path="string" URI模式 路径信息

          android:pathPattern="string"

          android:pathPrefix="string"

          android:mimeType="string"/> 

具体使用:

Intent intent = new Intent("android.intent.action.SEND.MULTIPLE");

intent.addCategory("android.intent.category.DEFAULT");

intent.setDataAndType(Uri.parse("content://com.example.demo:80/file"),"image/*");

startActivity(intent);

举例打开QQ

String url="mqqwpa://im/chat?chat_type=wpa&uin=771346371";

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

举例打开简书

String url = "intent://notes/56ea25fbe5cf/#Intent;scheme=jianshu;package=com.jianshu.haruki;end"

startActivity(Intent.parseUri(url,0));

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容