Intent在Android中的重要性不言而喻。本文主要总结下Intent使用过程中需要注意的一些问题。

1.隐式Intent AndroidManifest.xml声明时<intent-filter>相关

作为“意图”的Intent,在AndroidManifest.xml声明时并没有独立的所谓的<intent>标签形式,而是依附于其他的应用程序组件(Activity/BroadcastReceiver/Service)存在。在显式Intent和隐式Intent类别上,显式Intent直接对应组件名称,隐式Intent则对应组件声明中的子节点<intent-filter>而存在。

<intent-filter>声明时需要注意如下几点:

1).<intent-filter>意为"intent匹配器",主要能够通过此“intent匹配器”的目标组件,都是“意图”的目标对象,对于启动Activity的隐式Intent,如果有多个目标Activity均可满足,将以列表弹框弹出以供用户选择具体启动对象。在<intent-filter>声明时,主要包括<action>,<category>和<data>子标签。

2).<action>作为“意图”的具体动作,在<intent-filter>中是必须要有的,一个<intent-filter>声明中可以有多个<action>子标签,表示可以匹配多个不同的相应intent action,action的具体命名最好以包名开头,以确保action的唯一性;

3).<category>作为"意图"的类别,针对隐式Activity组件,action在<intent-filter>中也是必须要有的。默认情况下,以隐式Intent方式启动Activity时系统会自动加上category “android.intent.category.DEFAULT”。这样,就导致<intent-filter>中必须至少包含有<category android:name="android.intent.category.DEFAULT" />的声明。当且仅当如下情况除外:

1 <action android:name="android.intent.action.MAIN" />
2 <category android:name="android.intent.category.LAUNCHER" />

下面是关于Android文档中关于category.DEFAULT具体警示:

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter.
The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category.
If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.
Android automatically applies the the CATEGORY_DEFAULT category to all implicit intents passed to startActivity() and startActivityForResult().
So if you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT" in its intent
filters (as shown in the previous <intent-filter> example.

同样的,一个<intent-filter>声明中可以有多个<category>子标签。

4).一个Activity/BroadcastReceiver/Service组件声明中可以包含多个<intent-filter>标签,匹配时,针对单个的<intent-filter>依次进行匹配,总体原则是,单个<intent-filter>标签内部,各子标签应能够包含代码中intent各条件,可以多余但不能少。

2.代码中使用Intent启动其他的应用程序组件(Activity/BroadcastReceiver/Service)

1).对于显式Intent,直接指定目标组件的类名,系统会在当前App内部Intent到目标组件;

2).一个Intent中既用了显式Intent,同时又用了隐式Intent,直接以显式Intent为准;

3).隐式Intent必须通过如setAction(..)方法指定action,有此可见,隐式Intent代码中action唯一,通过addCategory (..)方法指定类别,由于category可以有多个;

4).App内部的Service启动最好使用显式Intent,原因Android文档中描述:

To ensure your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. 
Using an implicit intent to start a service is a security hazard because you cannot be certain what service will respond to the intent,
and the user cannot see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService()
with an implicit intent.

有此可见:Android 5.0开始,跨App的bindService将不再有效。

5).鉴于广播接收器本身就是由来解耦,因此,发送广播时只能使用隐式Intent;

6).当指定的Activity组件没有找到时,会抛出ActivityNotFoundException异常并直接崩溃,此时可以通过resolveActivity(..)方式先判断下:

if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}

7).判断两个Intent“意图”是否相同,使用filterEquals(intent)方法,注意只包括intent匹配时的描述信息,并不知extra和flag等额外信息。

filterEquals(Intent other)
Determine if two intents are the same for the purposes of intent resolution (filtering).
That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
filterHashCode()
Generate hash code that matches semantics of filterEquals().

Android Intent的更多相关文章

  1. android Intent介绍

    Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...

  2. android:Intent匹配action,category和data原则

    1.当你在androidmanifest里面定义了一个或多个action时 你使用隐式意图其他activity或者service时,规定你隐式里面的action必须匹配XML中定义的action,可以 ...

  3. android intent和intent action大全

    1.Intent的用法:(1)用Action跳转1,使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了 ...

  4. Android总结篇系列:Android Intent

    Intent在Android中的重要性不言而喻.本文主要总结下Intent使用过程中需要注意的一些问题. 1.隐式Intent AndroidManifest.xml声明时<intent-fil ...

  5. 什么时候加上android.intent.category.DEFAULT

    什么时候加上android.intent.category.DEFAULT 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent ...

  6. (转)android.intent.action.MAIN与android.intent.category.LAUNCHER

    android.intent.action.MAIN决定应用程序最先启动的Activity android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里 在网上看到 ...

  7. android之android.intent.category.DEFAULT的用途和使用

    1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...

  8. 理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER

    刚才看了一下sundy的视频<LLY110426_Android应用程序启动>,里面讲到luncher这个activity通过获取应用程序信息来加载应用程序,显示给用户,其中就是通过一个应 ...

  9. Android Intent的几种用法全面总结

    Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...

随机推荐

  1. 夺命雷公狗-----React---28--小案例之react经典案例todos(全选和反选)完

    这个功能实现的步骤如下所示: 最终实现全选和反选,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> ...

  2. 关于在linux中使用图形界面的网络管理工具

    有好几种自动设置的工具可以选择(既然说是自动设置的工具,那就说明有手动设置的工具,例如 使用 ip, iw, iwconfig 这些命令设置网络),例如:Connman, netctl, Networ ...

  3. DELPHI XE5 与SQLITE

    最近一次使用DELPHI做项目是使用DELPHI2009,为了访问本地数据库方便,使用ACCESS数据库,不需要安装驱动,(WINDOWS自带),但是ACCESS数据库的性能确实很糟糕,通过ADO连接 ...

  4. Java图形化用户界面(GUI)笔记(一)介绍

    一.布局 1.组件 以图形化的方式显示在屏幕上的,和用户进行交互的对象,就叫做组件. 组件包括:菜单.标签.文本框(密码框).滚动条.单选.复选.按钮等等. 2.容器 组件不能独立的显示出来,只能将组 ...

  5. [综合]visio2013安装提示找不到Office.zh_cn\officeMUI.mis officemui.xml

    在visio2013安装提示找不到Office.zh_cn\officeMUI.mis officemui.xml 等等,各种提示,可是打开安装程序所在的ISO,里面可是“赶集网”——啥都有啊.上网查 ...

  6. C# 隐藏标题栏 调整大小 并且移动窗口

    隐藏标题栏(窗口属性): 1.设置在该窗体的标题栏中是否显示控件框: this.ControlBox = false; 2.设置在该窗体的标题为空: this.Text = string.Empty; ...

  7. jade模板

    jade 模板使用 npm install jade -g      安装到全局 jade index.jade         导出一个 index.html 压缩后的 jade -P index. ...

  8. mha配置参数详解

    mha配置参数详解: 参数名字 是否必须 参数作用域 默认值 示例 hostname Yes Local Only - hostname=mysql_server1, hostname=192.168 ...

  9. Nodejs基础中间件

    Nodejs基础中间件Connect   http://www.tuicool.com/articles/emeuie 关于作者 张丹(Conan), 程序员Java,R,PHP,Javascript ...

  10. 关于sql 的列转行

    select * from TbaleOne unpivot(qty ])) as up go select * from TableTwo unpivot(grate for subject in ...