Android总结篇系列:Android Intent
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" />的声明。当且仅当如下情况除外:
<action android:name="android.intent.action.MAIN" />
<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总结篇系列:Android Intent的更多相关文章
- 【转】Android总结篇系列:Activity Intent Flags及Task相关属性
[转]Android总结篇系列:Activity Intent Flags及Task相关属性 同上文一样,本文主要引用自网上现有博文,并加上一些自己的理解,在此感谢原作者. 原文地址: http:// ...
- Android提升篇系列:Activity recreate(Activity 重新创建/自我恢复)机制(一)
注:本文中的recreate是指当内存不足时,Activity被回收,但再次来到此Activity时,系统重新恢复的过程.例如:当Activity A到Activity B时,如果内存不足,A被回收, ...
- Android总结篇系列:Activity中几个主要函数详解
Activity作为Android系统中四大基本组件之一,包含大量的与其他的各大组件.intent.widget以及系统各项服务等之间的交互的函数.在此,本文主要选取实际项目开发中常用的,但完全理解又 ...
- 【转】Android总结篇系列:Activity启动模式(lauchMode)
[转]Android总结篇系列:Activity启动模式(lauchMode) 本来想针对Activity中的启动模式写篇文章的,后来网上发现有人已经总结的相当好了,在此直接引用过来,并加上自己的一些 ...
- 【转】Android总结篇系列:Activity生命周期
[转]Android总结篇系列:Activity生命周期 Android官方文档和其他不少资料都对Activity生命周期进行了详细介绍,在结合资料和项目开发过程中遇到的问题,本文将对Activity ...
- Android总结篇系列:Activity Intent Flags及Task相关属性
同上文一样,本文主要引用自网上现有博文,并加上一些自己的理解,在此感谢原作者. 原文地址: http://blog.csdn.net/liuhe688/article/details/6761337 ...
- Android总结篇系列:Android Service
Service通常总是称之为“后台服务”,其中“后台”一词是相对于前台而言的,具体是指其本身的运行并不依赖于用户可视的UI界面,因此,从实际业务需求上来理解,Service的适用场景应该具备以下条件: ...
- Android总结篇系列:Activity启动模式(lauchMode)
本来想针对Activity中的启动模式写篇文章的,后来网上发现有人已经总结的相当好了,在此直接引用过来,并加上自己的一些理解,在此感谢原作者. 文章地址: http://blog.csdn.net/l ...
- Android总结篇系列:Android广播机制
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android总结篇系列:Android 权限
权限是一种安全机制.Android权限主要用于限制应用程序内部某些具有限制性特性的功能使用以及应用程序之间的组件访问.在Android开发中,基本上都会遇到联网的需求,我们知道都需要加上联网所需要的权 ...
随机推荐
- 使用maven下载jar包的source和javadoc
使用maven菜单下载sources和javadocs没什么反应,还是命令给力. 使用参数下载源码包与doc包: -DdownloadSources=true 下载源代码jar -DdownloadJ ...
- 35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n); (2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方 法时,要求计算1到n的和; (3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口 方法时,要求计算n的阶乘(n
35.按要求编写Java程序: (1)编写一个接口:InterfaceA,只含有一个方法int method(int n): (2)编写一个类:ClassA来实现接口InterfaceA,实现in ...
- MySQL(五) MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
- 元素多层嵌套,JS获取问题
如果一段html嵌套过多,在js中获取还是比较麻烦的,我写了几套方案,大家可以参考参考,如果你有好的方法,也分享出来,让我们瞧瞧. HTML: <!DOCTYPE html> <ht ...
- Open Cascade DataExchange IGES
Open Cascade DataExchange IGES eryar@163.com 摘要Abstract:本文结合OpenCascade和Initial Graphics Exchange Sp ...
- HTML内联元素
前面的话 用于标记段落里的文本和其他内容组的元素种类很多,本文将这些文本级元素进行简单分类,便于整理和记忆 通用容器 <span>元素是短语内容的通用行内容器,并没有任何特殊语义.可以使用 ...
- poj2513Colored Sticks(无向图的欧拉回路)
/* 题意:将两端涂有颜色的木棒连在一起,并且连接处的颜色相同! 思路:将每一个单词看成一个节点,建立节点之间的无向图!判断是否是欧拉回路或者是欧拉路 并查集判通 + 奇度节点个数等于2或者0 */ ...
- dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算
一.基本概念 dip : Density independent pixels ,设备无关像素. dp :就是dip px : 像素 dpi :d ...
- spring源码分析之spring-core-io
1. 看一下源码整体结构: 抓住主要点Resource.ResourceLoader和ResourceEditor 其中Resource作用 Interface for a resource desc ...
- js IndexedDB:浏览器端数据库的demo实例
IndexedDB具有以下特点. (1)键值对储存. IndexedDB内部采用对象仓库(object store)存放数据.所有类型的数据都可以直接存入,包括JavaScript对象.在对象仓库中, ...