Android中Action、Category、Data匹配规则

2019-05-31 19:39:38

Android 中隐式启动组件的匹配规则主要是根据 Action、Category、Data来匹配。

Action:定义匹配动作,属性值为一个字符串

Category:属性用于指定当前动作(Action)被执行的环境。

Data:用于指定数据,通常是URI格式



Action匹配只要有一个与Intent中携带Action相同即可:

<activity android:name=".androidImplicitFilter.ActionActivity">

            <intent-filter>

                <action android:name="com.action.a" />

                <action android:name="com.action.b" />

                <action android:name="com.action.c" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

</activity>

Intent中只要携带"com.action.a"、"com.action.b"、"com.action.c"中的一个就可以启动ActionActivity





Category匹配要包含全部Intent中携带Category

Category是一个字符串,系统中预定义了一些Category,也可以自定义Category

Intent intent = new Intent("com.action.a");

        intent.addCategory("com.category.a");

        intent.addCategory("com.category.b");

        startActivity(intent);

intent包含2个Category ,"com.category.a"、"com.category.b"。要启动的组件必须包含这两个Category

<activity android:name=".androidImplicitFilter.ActionActivity">

            <intent-filter>

                <action android:name="com.action.a" />

                <action android:name="com.action.b" />

                <action android:name="com.action.c" />



                <category android:name="android.intent.category.DEFAULT" />

                <category android:name="com.category.a" />

                <category android:name="com.category.b" />

                <category android:name="com.category.c" />

            </intent-filter>

        </activity>

Intent默认会携带"android.intent.category.DEFAULT",所以隐式启动的组件必须包含 <category android:name="android.intent.category.DEFAULT" />





Data匹配与Action相同

Data匹配规则与Action类似,Data一般由两部分组成mimeType和URI,mineType指资源类型包括文本、图片、音视频等等

URI结构如下:scheme://host:port/(path|pathPrefix|pathPatten)

例如 http://www.myHost:8080/user/index.html

    content://com.download/file/10089

scheme:URI的协议例如http、file、content,如果URI没有指定scheme,那么这个URI即使有其他部分也是无效的

host:域名,如果未指定那么也没效果,URI也就无效

port:端口号,scheme和host指定时才有效

path|pathPrefix|pathPatten:包含路径信息



假如我们要通过响应一个超连接打开相应的组件,我们可以这样实现:

 <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:host="myHost.app"

                    android:scheme="myScheme" />

</intent-filter>



<action android:name="android.intent.action.VIEW" /> 系统根据不同的Data类型,通过筛选出已注册的Application来显示数据

<category android:name="android.intent.category.DEFAULT" />默认的Category ,隐式启动的组件必须包含

<category android:name="android.intent.category.BROWSABLE" />设置该组件可以使用浏览器启动

Data响应协议为myScheme域名为myHost.app,即“myScheme://myHost.app”,还可以设置port、path等信息,更精确的过滤

这样既可通过点击超链接打开组件,注意微信等一些其他app对浏览器做了修改,会屏蔽这些事件



  <activity

            android:name="XXXActivity"

            android:screenOrientation="portrait"

            android:theme="@style/myTheme"

            android:windowSoftInputMode="adjustResize|stateHidden">

            <intent-filter>

                <action android:name="android.intent.action.VIEW" />

                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="application/pdf" />

                <data android:mimeType="application/msword" />

                <data android:mimeType="text/html" />

                <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

            </intent-filter>

 </activity>

 当前注册的Activity组件就是一个可以打开html、pdf、doc、docx文件的组件



 <action android:name="android.intent.action.VIEW" />

 <action android:name="android.intent.action.SEND" />

 就是我们通常在微信或者QQ中遇到的使用第三方应用打开或者本地文件选择“发送”选项的场景,这里的data过滤使用了mimeType,当然也可以同时使用mimeType和URI

 <intent-filter>

                 <action android:name="android.intent.action.VIEW" />

                 <action android:name="android.intent.action.SEND" />

                 <category android:name="android.intent.category.DEFAULT" />

                 <data android:mimeType="text/html"

                       android:host="myHost.app"

                       android:scheme="myScheme"

                       android:port=8888

                       android:path="xxxxx"/>

</intent-filter>



  • 2017-01-16 15:09:40

    Javascript模块化编程(一):模块的写法

    随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂。网页越来越像桌面程序,需要一个团队分工协作、进度管理、单元测试等等......开发者不得不使用软件工程的方法,管理网页的业务逻辑。

  • 2017-01-16 15:16:24

    Javascript模块化编程(二):AMD规范

    AMD是"Asynchronous Module Definition"的缩写,意思就是"异步模块定义"。它采用异步方式加载模块,模块的加载不影响它后面语句的运行。所有依赖这个模块的语句,都定义在一个回调函数中,等到加载完成之后,这个回调函数才会运行。

  • 2017-01-16 15:19:24

    RequireJS 入门指南

    如今最常用的JavaScript库之一是RequireJS。最近我参与的每个项目,都用到了RequireJS,或者是我向它们推荐了增加RequireJS。在这篇文章中,我将描述RequireJS是什么,以及它的一些基础场景。

  • 2017-01-16 15:22:30

    CommonJS规范

    CommonJS模块规范。 根据这个规范,每个文件就是一个模块,有自己的作用域。在一个文件里面定义的变量、函数、类,都是私有的,对其他文件不可见。

  • 2017-01-17 15:42:03

    vue-cli 发布(译)

    当我们真正开发一个应用的时候,我们不可避免的会用到一大堆的工具,模块化、预处理器、热模块加载、代码校验和测试。这些工具对于一个需要长期维护的大型应用是必须的,但是项目初始化将会是让人痛苦的事情。这就是为什么我们做了 vue-cli 。