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>



  • 2021-04-15 10:07:49

    Chrome屏蔽Your connection is not private

    使用Fiddler时如何屏蔽Chrome的证书警告:"Your connection is not private"/"您的连接不是私密连接"(如图1所示)? 启动chrome的时候加上--ignore-certificate-errors命令行参数(如图2所示)即可。

  • 2021-04-15 10:10:00

    Puppeteer 系列踩坑日志—3—开启支持插件

    在使用puppeteer自动化的过程中,会发现其实开启的chrome往往自动禁用了插件功能,如果我们想在自动化测试的过程中,再去使用一些常用的插件提升效率(偷懒)的话,就行不通了,其实解决办法还是有的,我们今天就来讲解这个问题。

  • 2021-04-15 10:11:17

    Puppeteer拦截修改返回值

    page.setRequestInterception(true)拦截器的使用方法和场景 现附上Puppeteer的Api的链接https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md

  • 2021-04-15 10:32:18

    怎么给 headless chrome添加cookies

    In puppeter you have access to the session cookies through page.cookies(). So once you log in, you could get every cookie and save it in a json file:

  • 2021-04-15 10:51:21

    如何通过Devtools协议拦截和修改Chrome响应数据

    在日常研究中,我们经常碰到大量JavaScript代码,我们首先要深入分析才能了解这些代码的功能及具体逻辑。这些代码代码可能会被恶意注入到页面中,可能是客户送过来需要我们帮忙分析的脚本,也可能是我们的安全团队在网页上找到的引用了我们服务的某些资源。这些脚本通常代码量不大、经过混淆处理,并且我们总是需要经过多层修改才能继续深入分析。

  • 2021-04-19 10:54:39

    block和delegate的区别

    代理 可读性高 大部分可以属性 block 写的代码少 一般作为参数 通知 占用资源