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-02-17 17:11:24

    mysql插入数据后返回自增ID的方法c

    产生的ID 每次连接后保存在服务器中。这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值的。这个值不能被其它客户端影响,即使它们产生它们自己的 AUTO_INCREMENT值。这个行为保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁或处理

  • 2017-02-21 07:59:45

    不会被 iOS 停掉的网页定时器

    其实这个标题略微有点标题党:iOS 中,除了少数服务(如播放音乐),大部分 App 在用户按了 Home 键之后,过不了多久就会被完全冻结,这对 Safari 同样适用。本文不考虑这样情况,只考虑 Safari 运行时,怎样让定时器持续工作。

  • 2017-02-24 15:04:10

    PHP 中的 9 个魔术方法

    这个标题有点牵强因为php有不只9种魔术方法, 但是这些将会引导你使用php魔术方法一个好的开始。它可能魔幻,但是并不需要魔杖。 这些'魔术'方法拥有者特殊的名字,以两个下划线开始,表示这些方法在php特定事件下将会被触发。这可能听起来有点自动魔法但是它真的很酷的,我们已经看过一个简单的例子在 last post,即我们使用一个构造器-使用这个作为我们第一个例子

  • 2017-02-24 15:06:18

    PHP 中的 9 个魔术方法

    这个标题有点牵强因为php有不只9种魔术方法, 但是这些将会引导你使用php魔术方法一个好的开始。它可能魔幻,但是并不需要魔杖。 这些'魔术'方法拥有者特殊的名字,以两个下划线开始,表示这些方法在php特定事件下将会被触发。这可能听起来有点自动魔法但是它真的很酷的,我们已经看过一个简单的例子在 last post,即我们使用一个构造器-使用这个作为我们第一个例子

  • 2017-02-24 15:07:05

    PHP 中的 9 个魔术方法

    这个标题有点牵强因为php有不只9种魔术方法, 但是这些将会引导你使用php魔术方法一个好的开始。它可能魔幻,但是并不需要魔杖。 这些'魔术'方法拥有者特殊的名字,以两个下划线开始,表示这些方法在php特定事件下将会被触发。这可能听起来有点自动魔法但是它真的很酷的,我们已经看过一个简单的例子在 last post,即我们使用一个构造器-使用这个作为我们第一个例子

  • 2017-02-24 15:53:02

    PHP中__get()和__set()的用法实例详解

    在PHP5中,预定义了两个函数“__get()”和“__set()”来获取和赋值其属性,对每个字段进行set和get的操作。只需要加上两个魔术方法即可