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. ...
随机推荐
- solr与mysql数据同步的方案
1.使用activeMQ http://blog.csdn.net/zhou2s_101216/article/details/77855413 2.通过配置实现定时同步 http://blog.cs ...
- vmware:Could not open /dev/vmmon: No such file or directory.
Q: Could not open /dev/vmmon: No such file or directory. Please make sure that the kernel module `vm ...
- html5 canvas文本处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CMSZU站群管理系统 升级到 v1.8 [源码下载]
CmsZu 简介 CMSZU即CMS族,是个网站内容管理平台,基于PHP+MYSQL技术创建,源码开放. CmsZu 更新说明 V1.8 修改了些bug 完善数据库管理 -> 数据库表管理的 字 ...
- 【CodeForces】925 C.Big Secret 异或
[题目]C.Big Secret [题意]给定数组b,求重排列b数组使其前缀异或和数组a单调递增.\(n \leq 10^5,1 \leq b_i \leq 2^{60}\). [算法]异或 为了拆位 ...
- 记webpack下提取公共js代码的方法
环境: webpack4.6 + html-webpack-plugin 多页面多入口 经多次研究,稍微靠谱可用的配置 optimization: { splitChunks: { minSize: ...
- Hibernate5总结
1. 明确Hibernate是一个实现了ORM思想的框架,它封装了JDBC,是程序员可以用对象编程思想来操作数据库. 2. 明确ORM(对象关系映射)是一种思想,JPA(Java Persistenc ...
- 联通-长春处,FDD和TDD宏站,数据业务接入时延期望值默认值应为80ms
有小坑 备注:若已经跑过V5.40.00_Alpha1_Baseline.sql或V5.30.02_Beta_TO_V5.40.00_Alpha1.sql的脚本,再次运行升级脚本修改不成功,需手动在数 ...
- cefsharp保存文件为pdf
var success = await browserViewModel.WebBrowser.PrintToPdfAsync(dialog.FileName, new PdfPrintSetting ...
- 面试经典---数据库索引B+、B-树
大型数据库数据都是存在硬盘中的,为了操作的速度,需要设计针对外存的数据结构.而数据库索引技术就是在面试中反复被问到的一个问题:数据库索引是怎么实现的?数据库索引越大越好吗? 需要详细了解下这方面的知识 ...