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" />的声明。当且仅当如下情况除外:

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(..)方式先判断下:

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

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().

 

---------------------------------------------------------------------------------
笔者水平有限,若有错漏,欢迎指正,如果转载以及CV操作,请务必注明出处,谢谢!

分类: Android


本文转自Windstep博客园博客,原文链接:http://www.cnblogs.com/lwbqqyumidi/p/4195679.html,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
Android开发 开发者
Android基础知识:什么是Intent?有哪些类型的Intent?
Android基础知识:什么是Intent?有哪些类型的Intent?
50 0
|
6月前
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
存储 缓存 前端开发
关于Android SurfaceView截屏总结
关于Android SurfaceView截屏总结
1441 0
|
9月前
|
Android开发
关于Android中intent传值问题
关于Android中intent传值问题
57 0
|
4月前
|
Android开发
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
43 0
|
8月前
|
Java API Android开发
Android 中Activity和Intent的详解
Android 中Activity和Intent的详解
75 0
|
8月前
|
Java Android开发
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
76 1
|
编解码 Android开发
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
475 0
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
|
10月前
|
Java API 文件存储
Android:常用的隐式 Intent
本篇文章的内容其实是属于上一篇文章(Android 基础知识5:Intent 和 Intent 过滤器)的延伸,考虑到篇幅长度的原因,所以没有把本篇文章的内容写到上一篇文章中,另外单独写成一篇文章还有个好处就是方便读者查阅。这篇文章主要为大家列举了常用的隐式 Intent,大家如果在平时工作中有相关的需求可以直接复制代码使用。
67 0
Android:常用的隐式 Intent
|
10月前
|
XML 存储 前端开发
Android:Intent 和 Intent 过滤器
在前 4 篇文章中,我们介绍了 Android 四大组件的基础知识,四大组件是构成我们 App 的基础,也是 Android 系统设计的最佳体现。各个组件之间完全是解耦的,如果想访问其他组件或者启动其他组件可以使用 Intent 来操作。在四种组件类型中,有三种(Activity、Service 和 Broadcast)均可以通过异步消息 Intent 进行启动。Intent 会在运行时对各个组件进行互相绑定。所以我们可以把 Intent 当作是各个组件之间的信使(无论该组件是自己 App 的还是其他 App)。
51 0
Android:Intent 和 Intent 过滤器