Android-IntentFilter
Android-IntentFilter
学习自
《Android开发艺术探索》
IntentFilter漫谈
众所周知,在Android中如果要想启动一个Activity,有两种方式,显式启动 隐式启动 ,显式启动不用多说,隐式启动才是我们本章需要学习的重点。
我们开发的程序,常常需要和其他的APP进行交互,比如说,我们有一个程序,需要快捷拨号,这时候我们就需要启动系统的拨号界面来完成拨号操作了,要是一个程序能够随随便便地就能启动其他程序,那也忒不安全了,如果想要 隐式启动 Activity,必须满足对应的Activity添加的IntentFilter(意图过滤器)。
IntentFilter主要分为以下几个部分并且均可包含多个且一个Activity可以拥有多个IntentFilter:
- action
- category
- data
<activity android:name=".Test2Activity">
<intent-filter>
<action android:name="com.example.it.studykotlin.Test2Activity_1" />
<action android:name="com.example.it.studykotlin.Test2Activity_2" />
<category android:name="com.example.it.studykotlin.Test2Activity_1" />
<category android:name="com.example.it.studykotlin.Test2Activity_2" />
<data android:mimeType="text/plain" />
</intent-filter> <intent-filter>
<action android:name="com.example.it.studykotlin.Test2Activity_3" />
<action android:name="com.example.it.studykotlin.Test2Activity_4" />
<category android:name="com.example.it.studykotlin.Test2Activity_3" />
<category android:name="com.example.it.studykotlin.Test2Activity_4" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Action
一般在建立IntentFilter的时候,我们首先会对Acting进行设置,表示,限定要执行那些操作的人才能跳转我们所创建的Activity。
Action 是一个普通的字符串,系统已经定义了一些Action,如果系统定义的不能满足我们的需要的话,我们同样可以自定义Action,Action的匹配规则如下: Intent中的Action的只要与IntentFilter中定义的任意一个Action匹配成功即可。
Demo
定义Activity并通过IntentFilter的Action来过滤。
<activity android:name=".TestActivity">
<intent-filter>
<action android:name="com.example.it.studyandroid.send" />
</intent-filter>
</activity>
Intent设置Action
val tempIntent = Intent()
tempIntent.action = "com.example.it.studyandroid.send"
Category
如果想要建立的Activity能够被隐式启动的话,Category也是必须设置的,有些时候我们可能并不需要Category,但是又有强制的限制必须要设置Category,这时候我们可以将Category设置成为 android.intent.category.DEFAULT ,在使用Intent启动Activity的时候,如果没有设置Category那么系统会为我们设置这个默认的Category。
Category 同样可以是任意的字符串,Category的匹配规则是,如果在Intent中定义了Category,那么Intent中所有所有的Category必须和IntentFilter中的Category其中之一相匹配。
Demo
IntentFilter设置Category
<activity android:name=".TempActivity">
<intent-filter>
<action android:name="com.example.it.studyandroid.send" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
为Intent添加Activity
val tempIntent = Intent()
tempIntent.action = "com.example.it.studyandroid.send"
//因为IntentFilter中定义的Default Category,所以这句话不写也可以1启动Activity
tempIntent.addCategory(Intent.CATEGORY_DEFAULT)
this.startActivity(tempIntent)
Data
Data 这一筛选条件比较复杂,分为以下几个部分:

Data是由URI和mimeType两大部分构成的,通过Data我们可以限制,Intent传递过来的数据,更为精确的要求了,启动对应Activity的条件。URI 用力来传递数据,而mineType用来定义数据的类型。
#### URI
一些URI的DEMO
```kotlin
content://com.example.project:200/folder/subfolder/etc
https://www.baidu.com/
```
- __schedule(协议) __URI的协议,常见的有
httpfilecontent等,schedule是必须的,如果没有schedule那么URI无效 - __host(主机名) __URI中的主机名,例如
www.baidu.com如果没有host,那么URI无效 - port(端口号),这个就无需解释了
- path/pathPattern, 路径信息,其中path 不支持通配符而pathPattern支持通配符
- pathPrefix: 设置路径的前缀,比如说我的博客https://www.cnblogs.com/slyfox/p/9234790.html 那么只要设置了 pathPrefix为
/slyfox即可。
Data的匹配规则
例子1:
<activity android:name=".TempActivity">
<intent-filter>
<action android:name="com.example.it.studyandroid.send" />
<category android:name="android.intent.category.DEFAULT" />
<!--定义的mimeType-->
<data android:mimeType="image/*" />
</intent-filter>
</activity>
上面的代码片段中虽然仅仅是指定了 mimeType 并没有指定URI的信息,但是URI却又默认值 schedule部分必须是 file 或者 content 才能匹配成功。
val tempIntent = Intent()
tempIntent.action = "com.example.it.studyandroid.send"
//如果需要同时设置data和mimiType,那么必须使用此方法才行,
// 否则使用setData和setType方法的话会相互抵消
tempIntent.setDataAndType(Uri.parse("content://temp"), "image/*")
this.startActivity(tempIntent)
例子2:
<activity android:name=".TempActivity">
<intent-filter>
<action android:name="com.example.it.studyandroid.send" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="www.baidu.com"
android:mimeType="video/mpeg"
android:scheme="http" />
<data
android:host="www.baidu.com"
android:mimeType="audio/mpeg"
android:scheme="http" />
</intent-filter>
</activity>
启动
tempIntent.setDataAndType(Uri.parse("http://www.baidu.com"), "video/mpeg")
tempIntent.setDataAndType(Uri.parse("http://www.baidu.com"), "audio/mpeg")
避免隐式启动Activity报错
当我们使用隐式启动Activity的时候,经常用以报错,所以我们在启动Activity的时候需要事先检查一下是否存在指定的Activity。相关方法如下:
- packageManager的resolveActivity 方法
- intent的resolveActivity方法
- packageManager的queryIntentActivities方法
其中,intent的resolve不多说了,参数比较简单所以我就不在这里多说,接下来我们来看看其他两个方法的参数
/**
@param intent 要检查的Intent对象
@param flags 标识位,指定匹配哪一类的Activity
*/
public abstract ResolveInfo resolveActivity(Intent intent, @ResolveInfoFlags int flags);
//此方法同上,只不过返回的是一个集合
public abstract List<ResolveInfo> queryIntentActivities(Intent intent,
@ResolveInfoFlags int flags);
调用方法
if (packageManager.resolveActivity(tempIntent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
Toast.makeText(this, "The Activity is not existed!", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
其中flags标志为常用的标志是 MATCH_DEFAULT_ONLY 标识仅仅匹配category中定义了 android.intent.category.DEFAULT 的Activity,这就保证了只要返回值不为空,那么久一定能够成功启动Activity。
Android-IntentFilter的更多相关文章
- android IntentFilter 使用之 data过滤
1 Intent分为两大类,显式和隐式. 显式事件,就是指通过 component Name 属性,明确指定了目标组件的事件. 比如我们新建一个Intent,指名道姓的说,此事件用于启动名为" ...
- Android intent-filter 简单用法
对电话拨号盘的过滤,mainfest配置文件中Activity如下配置: <activity Android:name=".TestActivity" android:lab ...
- Android IntentFilter匹配规则
一:显式调用 需要明确指定被启动对象的组件信息,一般是在相同的应用程序内部实现的 Intent intent = new Intent(); intent.setClass(SecondActivi ...
- Android IntentFilter 匹配原则浅析
1 Intent分为两大类,显式和隐式. 显式事件,就是指通过 component Name 属性,明确指定了目标组件的事件. 比如我们新建一个Intent,指名道姓的说,此事件用于启动名为" ...
- android intent-filter 注册网页链接打开app
如下实现注册m.hao123.com的链接: <intent-filter><category android:name="android.intent.category. ...
- [转]android笔记--Intent和IntentFilter详解
Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介. 使用Intent启动组件的优势1, Intent为组件的启 ...
- (备忘)android清单文件中<meta-data>标签,以及<intent-filter>下的<data>标签及其他标签
1.metadata可以写在application下也可以写在activity下,作为全局或activity内共享的数据 以键值对形式保存 <meta-data android:name=&qu ...
- Android,配置Activity为启动Activity(AndroidManifest.xml,application,intent-filter,MAIN,LAUNCHER)
备忘: 将Activity注册为启动Activity. 在AndroidManifest.xml中的<application>元素中加入以下<activity>子元素内容: & ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- Android开发-API指南-<intent-filter>
<intent-filter> 英文原文:http://developer.android.com/guide/topics/manifest/intent-filter-element. ...
随机推荐
- debian8.4 ibus中文输入法
安装IBus: # apt-get install ibus ibus-sunpinyin ibus-table-wubi 导入输入法: 在Activities->Applications-&g ...
- 在线Python学习网站
目前我们使用的Python集成环境是Anaconda3,然后使用Jupyter Notebook和Spyder两个开发环境 Goole推出了在线的开发环境,在线网站: https://colab.re ...
- 在android手机上通过Html5Plus调用java类。
关于html5plus的资料参考http://www.html5plus.org/ 最近通过html5做手机app,其中涉及到网络通过,必须采用原生的socket,websocket无法满足要求,ht ...
- 求二叉树中第K层结点的个数
一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...
- 第一、介绍Canvas
canvas能做什么? canvas是HTML5中的新元素,你可以使用javascript用它来绘制图形.图标.以及其它任何视觉性图像.它也可用于创建图片特效和动画.如果你掌握了完整的命令,你可以用c ...
- HTML5自定义data属性
可能大家在使用jquery mobile时,经常会看到data-role.data-theme等的使用,比如:通过如下代码即可实现页眉的效果: [html] <div data-role=&qu ...
- Chrome插件:gitlab activity dashboard background-color
背景 我一般都是在activity dashboard页看同事的提交记录,这样只要我有权限的项目有人提交了我就能够知道,虽然提交的具体代码压根不看.......但至少能够了解各个项目的开发情况(如果大 ...
- 在vue-cli下读取模拟数据请求服务器
写此记录时vue脚手架的webpack是3.6.0 此文章方法亦可用于vue-cli3,直接在vue.config.js里面添加 本记录使用vue-resource,先安装: cnpm install ...
- webpack2.0学习
1.进到指定的目录下,新建自己的文件名 mkdir webpack-test 创建你的项目名称或者你己有的项目名称cd webpack-test npm initnpm install webpack ...
- npm tls证书报错
Error: unable to verify the first certificate 设置node环境(零时使用,再次使用需要重新使用) set NODE_TLS_REJECT_UNAUTHOR ...