概述:GitHub

IntentFilter意图过滤器,三种匹配规则:action、category、data
重点:过滤规则中必须设置 '<category android:name="android.intent.category.DEFAULT" />' ,否则不生效。
同样也说明了addCategory是一个叠加的属性。其源码内部是一个 ArraySet

先做几个演示:

入口Activity:

public class SelectFilterActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_filter);
} /**
* 通过自定义action跳转,不设置category(系统在startActivity/startActivityForResult时默认为Intent加上DEFAULT),设置相应的data
*
* @param view
*/
public void action1(View view) {
Intent intent = new Intent();
intent.setAction("com.action.123");
// intent.addCategory("");
//注意:不能先setData在setType,因为这两个方法彼此会清除对方的值,详见源码
intent.setDataAndType(Uri.parse("content://"), "text/plain");
startActivity(intent);
} /**
* 通过自定义action跳转,设置自定义category,设置相应的data
*
* @param view
*/
public void action2(View view) {
Intent intent = new Intent("com.action.1");
intent.addCategory("com.category.abc.2");
intent.setDataAndType(Uri.parse("http://www.baidu.com:8080"), "image/*");
startActivity(intent);
} /**
* 不设置action,设置自定义category,设置相应的data
   *
* @param view
*/
public void category1(View view) {
Intent intent = new Intent();
intent.addCategory("com.category.mif3");
intent.setDataAndType(Uri.parse("file://abc"), "video/*");
startActivity(intent);
}
}

相应的AndroidMenifest.xml中的过滤规则:(其中MyIntentFilterActivity1~5均为测试用的EmptyActivity)

<!-- Android意图过滤器 -->
<activity android:name=".Chapter1.intentfilter.SelectFilterActivity" />
<activity android:name=".Chapter1.intentfilter.MyIntentFilterActivity">
<intent-filter>
<!-- 区分大小写 -->
<action android:name="com.action.1" />
<action android:name="com.action.2" />
<!--
系统在startActivity或startActivityForResult时会默认为 Intent 加上DEFAULT ,
所以为了使我们新建的Activity能够接收隐式调用,就必须在intent-filter中添加DEFAULT的category属性
-->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.category.abc.1" />
<category android:name="com.category.abc.2" />
<!-- 注:如果过滤规则中定义了data,那么Intent中也必须要定义可匹配的data -->
<!-- scheme 默认为content和file -->
<!-- 不通data标签中的scheme、mimeType属性通用,host、port不通用 -->
<data
android:host="www.jooy.top"
android:mimeType="image/*"
android:port="80"
android:scheme="http" />
<data
android:host="www.baidu.com"
android:port="8080"
android:scheme="content" />
</intent-filter>
</activity>
<activity android:name=".Chapter1.intentfilter.MyIntentFilterActivity2">
<intent-filter>
<action android:name="com.action.123" />
<!-- 要么自定义,要么用DEFAULT,否则隐式跳转失败,并抛出异常 ActivityNotFoundException -->
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*" />
<data android:mimeType="audio/*" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity> <activity android:name=".Chapter1.intentfilter.MyIntentFilterActivity3">
<intent-filter>
<action android:name="com.action.mif3" />
<category android:name="android.intent.category.DEFAULT" /> <!-- 必须有 -->
<category android:name="com.category.mif3" />
<!-- scheme 默认值 content/file -->
<data android:mimeType="video/*" />
</intent-filter>
</activity>
<activity android:name=".Chapter1.intentfilter.MyIntentFilterActivity4">
<intent-filter>
MyIntentFilterActivity3
</intent-filter>
</activity>
<activity android:name=".Chapter1.intentfilter.MyIntentFilterActivity5">
<intent-filter>
MyIntentFilterActivity3
</intent-filter>
</activity>

回归正文:

问题描述:
在不指定具体action前提下(Intent跳转时可以不setAction,但清单文件中必须有action的过滤 <action android:name="com.action.mif3"/>),
如果有多个Activity:MyIntentFilterActivity4,5和MyIntentFilterActivity3等的intent-filter完全相同,
项目同步是否会出现异常?程序运行是否会崩溃?。
答案是程序不会崩溃,系统会从底部弹窗,按照清单文件注册Activity的顺序依次排列,Act3..Act4..Act5...,点击进入相应的Activity,
just like this:

可见,在隐式跳转中,如果不指定具体的action名称(字符串),而是intent仅通过category+data类型去找相应的activity的话,可能会返回来一个Activity的集合,也可能返回空,那么此时就需要PackageManager出场了。

PackageManager两个api:(1)resolveActivity 方法,如果找不到目标act就返回null。Intent中也有该API,效果相同;(2)queryIntentActivities方法:返回所有成功匹配的act信息。

而且,针对Service和BroadcastReceiver等组件,PM同样提供了类似的方法获取匹配信息,如queryIntentServices、queryBroadcastReceivers等方法。

还差点东西。。。

上面说列表显示顺序是按照menifest的配置顺序排列的,我们调换下顺序

MyIntentFilterActivity3→MyIntentFilterActivity4→MyIntentFilterActivity5

改为:

MyIntentFilterActivity5→MyIntentFilterActivity4→MyIntentFilterActivity3

GitHub

如果这篇随记对你有所帮助,请点个赞吧。发起进攻(  ´-ω ・)▄︻┻┳══━一

‧★,:*:‧\( ̄▽ ̄)/‧:*‧°★* 

Android DevArt4:IntentFilter学习及深入~问题描述:在不指定具体action前提下,如果有两个以上的Activity,具有完全相同的intent-filter,项目同步是否会出现异常?程序运行是否会崩溃?的更多相关文章

  1. Android Google官方文档(cn)解析之——Intents and Intent filter

    应用程序核心组件中的三个Activity,service,还有broadcast receiver都是通过一个叫做intent的消息激活的.Intent消息传送是在相同或不同的应用程序中的组件之间后运 ...

  2. Android从启动到程序运行整个过程的整理

    1Android是基于Linux的一个操作系统,它可以分为五层,下面是它的层次架构图,可以记一下,因为后面应该会总结到SystemServer这些Application Framework层的东西 A ...

  3. 关于C++程序运行程序是出现的this application has requested the runtime to terminate it in an unusual way. 异常分析

    今天运行程序是出现了this application has requested the runtime  to terminate it in an unusual way. 的异常报告,以前也经常 ...

  4. android: 将程序运行到手机上

    8.3.1   将程序运行到手机上 不必我多说,首先你需要拥有一部 Android 手机.现在 Android 手机早就不是什么稀罕 物,几乎已经是人手一部了,如果你还没有话,抓紧去购买吧. 想要将程 ...

  5. Qt opencv程序运行异常

    搭建了两次qt opencv vs ,经常出现程序运行异常.找了几个原因如下: 1.opencv的路径未配置或配置有误. 2.qt中pro文件包含不正确. 3.测试opencv程序不正确.如视频或图片 ...

  6. Android学习之Button按钮在程序运行时全部变大写的处理

    问题: 在layout布局文件中,我们命名的按钮名称是“button1”,程序运行过后,在app上显示出来的是“BUTTON1”,先看源代码和效果: 按钮源代码: 运行效果: 解决办法: 方法一: 在 ...

  7. GD32电压不足时烧写程序导致程序运行异常的解决方法

    一直使用的GD32F450前段时间遇到这样一个问题,当使用J-Link供电给板子烧写程序之后,程序运行缓慢,就像运行在FLASH高速部分之外一样,但是如果使用外部供电烧写,就不会出现这个问题,而且一旦 ...

  8. Android M Permission 学习笔记

    Android应用权限简要介绍 一个Android应用默认情况下是不拥有任何权限的, 这即是说, 在默认情况下, 一个应用是没有权利去进行一些可能会造成不好影响的操作的. 这些不好的影响可能是对其它应 ...

  9. Android系统源代码学习步骤

    目前,互联网行业正在朝着移动互联网方向强劲地发展,而移动互联网的发展离不开背后的移动平台的支撑.众所周知,如今在移动平台市场上,苹果的iOS.谷歌的Android和微软的Windows Phone系统 ...

随机推荐

  1. 绘制字母和数字组合的验证码(原生php)

    <?php $font = array('font/FZZQJW.TTF','font/STHUPO.TTF');//字体 $str = '689acdefhjkmnpqrtuvwxyACDEF ...

  2. 文件IO-Linux

    pcb:结构体 一个进程由一个文件描述符表:1024,前三个占用,文件描述符作用,需要磁盘文件. 1:open.close int open(const char* pathname,int flag ...

  3. scrapy框架之日志等级和请求传参-cookie-代理

    一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...

  4. sqlserver表数据的修改

    清除表数据  truncate table  [表名称] 将表b中的一列数据,更新到表tableA  如: tableA .key tableA .value 123   124   tableB.k ...

  5. QQ号码正则判断

    <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">   ...

  6. 类似openDialog的弹窗

    html <modal title="这里是标题" hidden="{{modalHidden}}" bindconfirm="modalCon ...

  7. angularjs的ng-class

    <!--第一种 直接加变量--> <div ng-class="tempClass"></div> <!--第二种 用{{}} 包住的变量 ...

  8. 初识vuejs

    转行前端,时间也不短了,也见识到了前端行业的蓬勃发展,以及一些新鲜技术的层出不穷. 由于自身计算机基础的薄弱,更加上一直没有遇上一个公司力推新技术,所以一直以来基本上都是靠着jquery和则zepto ...

  9. 【Selenium-WebDriver问题点】chromeDriver和chrome浏览器版本之间的兼容性问题

    今天早晨因为测试需求,将chrome浏览器更新到最新的65版本,结果之前用的chromeDriver测试计划,都跑不通过了, 所以就在网上找了下,mark下. 最新的chromedriver与chro ...

  10. official shiro(Reference Manual)

    Apache Shiro Reference Documentation Overview Core Spring-based Applications 1.Overview pom.xml < ...