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>



  • 2018-08-28 14:19:58

    php如何配置上传较大文件

     在php中判断上传文件的大小,但是文件一但过大,print_r($_FILES);的值就变为null了,有时候大家会遇到这么一个问题,上传小文件 时,PHP能正常获取到,但是文件一超过8M就变为空了,我在做项目的时候,就遇到这样的问题,我刚开始把最大上传文件值改为20M,在PHP.INI里 面。

  • 2018-08-28 15:09:50

    JAVA中MAP值保持顺序不变

    今天在进行JAVA开发过程中,因需要使用MAP来存放数据,同时希望MAP中KEY的顺序与放入顺序保持一致。 在使用HashMap之后,发现KEY的顺序是乱序的,每次打印还不太一样。上网查询资料之后发现:

  • 2018-09-03 13:57:27

    load average 的含义

    一般的进程需要消耗CPU、内存、磁盘I/O、网络I/O等资源,在这种情况下,平均负载就不是单独指的CPU使用情况。即内存、磁盘、网络等因素也可以影响系统的平均负载值。 在单核处理器中,平均负载值为1或者小于1的时候,系统处理进程会非常轻松,即负载很低。当达到3的时候,就会显得很忙,达到5或者8的时候就不能很好的处理进程了(其中5和8目前还是个争议的阈值,为了保守起见,建议选择低的)。

  • 2018-09-05 01:42:03

    git reset 回退以前某个版本及常用命令

    第一个命令只删除所有untracked的文件,如果文件已经被tracked, 修改过的文件不会被回退。而第二个命令把tracked的文件revert到前一个版本,对于untracked的文件(比如编译的临时文件)都不会被删除。

  • 2018-09-08 16:04:25

    Android SQLiteDatabase使用详细教程

    Android提供了创建和是用SQLite数据库的API。SQLiteDatabase代表一个数据库对象,提供了操作数据库的一些方法。在Android的SDK目录下有sqlite3工具,我们可以利用它创建数据库、创建表和执行一些SQL语句。下面是SQLiteDatabase的常用方法。SQLiteDatabase的常用方法方法名称方法表示含义openOrCreateDatabase(Stringpath,SQLiteDatabase.CursorFactoryfactory)