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开发中,基本上都会遇到联网的需求,我们知道都需要加上联网所需要的权 ...
随机推荐
- Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生
同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...
- 练习2 练习目标-使用引用类型的成员变量:在本练习中,将扩展银行项目,添加一个(客户类)Customer类。Customer类将包含一个Account对象。
package banking; public class Customer { private String firstName; private String lastName; private ...
- 学习bootstrap遇到的问题--001 关于bootstrap中类.disabled不禁用默认行为
自学bootstrap遇到的疑惑篇: 按钮状态--禁用 在Bootstrap框架中,要禁用按钮有两种实现方式: 方法1:在标签中添加disabled属性 方法2:在元素标签中添加类名"dis ...
- ios app 打包上传 app store(Application Loader)
背景:使用Xcode 上传APP, 这个 有时候很慢,构建版本需要等很长时间,所以我推荐使用Application Loader 1.使用xocd 打包,导出.ipa文件 2. OK ,跟着上面做, ...
- 快速入门系列--CLR--03泛型集合
.NET中的泛型集合 在这里主要介绍常见的泛型集合,很多时候其并发时的线程安全性常常令我们担忧.因而简述下.NET并发时线程安全特性,其详情请见MSDN. 普通集合都不支持多重并发写操作 部分支持单线 ...
- C++中如何定义类和对象?
在C++语言中,对象的类型被称为类,类代表了某一批对象的共性和特征. 类是对象的抽象,而对象是类的具体实例.如同C中的结构体一样,我们要先定义一个结构体,再使用结构体去定义一个变量.同一个结构体可以定 ...
- Linux下程序包管理工具RPM
实验环境: CentOS release 6.6 (Final) 一台 IP地址:172.16.249.230 RPM 是 Red Hat Package Manager 的缩写,本意是Red Ha ...
- Yii的学习(4)--Active Record
摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 ...
- 希望有兴趣的加入,共同为项目智能化管理jar包而努力 第二篇
想听听大家对于我这个想法的一些看法,喷也好,赞也罢,希望留下您宝贵的建议! 有共同想法并且想实现的请入群 2500261120 在使用autort插件时,首先要到autort服务器下载所有 ...
- RHEL6 某业务用户ulimit -a命令找不到
最终确定是shell环境问题,临时改为/bin/bash即可查看. 1.问题现象 # su - jingyu $ id uid=(jingyu) gid=(jingyu) (jingyu) $ uli ...