Android:Intent传递数据的几种类型和源码实现
- public class Intent implements Parcelable, Cloneable { //... private String mAction;
- private Uri mData;
- private String mType;
- private String mPackage;
- private ComponentName mComponent;
- private int mFlags;
- private HashSet<String> mCategories;
- private Bundle mExtras;
- private Rect mSourceBounds;
转自:http://blog.csdn.net/annkie/article/details/8483253
Intent也是继承了Parcelable的接口。
个人理解,Intent应该只是一个数据参数的载体,真正将两个Acitivity/Service通信起来的是Binder接口(C/S架构)。
第一类:简单或基本数据类型
- Intent putExtra(String name, int[] value)
- Intent putExtra(String name, float value)
- Intent putExtra(String name, byte[] value)
- Intent putExtra(String name, long[] value)
- Intent putExtra(String name, float[] value)
- Intent putExtra(String name, long value)
- Intent putExtra(String name, String[] value)
- Intent putExtra(String name, boolean value)
- Intent putExtra(String name, boolean[] value)
- Intent putExtra(String name, short value)
- Intent putExtra(String name, double value)
- Intent putExtra(String name, short[] value)
- Intent putExtra(String name, String value)
- Intent putExtra(String name, byte value)
- Intent putExtra(String name, char[] value)
- Intent putExtra(String name, CharSequence[] value)
本质上仍然是通过一个Bundle(private Bundle mExtras;)来实现:
- public Intent putExtra(String name, long value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putLong(name, value);
- return this;
- }
第二类:传递一个Bundle
- public Intent putExtra(String name, Bundle value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putBundle(name, value);
- return this;
- }
第三类:传递Serializable对象
- public Intent putExtra(String name, Serializable value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putSerializable(name, value);
- return this;
- }
第四类:Parcelable对象
- public Intent putExtra(String name, Parcelable value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putParcelable(name, value);
- return this;
- }
- public Intent putExtra(String name, Parcelable[] value) {
- if (mExtras == null) {
- mExtras = new Bundle();
- }
- mExtras.putParcelableArray(name, value);
- return this;
- }
第五类:Intent
- public Intent putExtras(Intent src) {
- if (src.mExtras != null) {
- if (mExtras == null) {
- mExtras = new Bundle(src.mExtras);
- } else {
- mExtras.putAll(src.mExtras);
- }
- }
- return this;
- }
归根结底都是通过Bundle来实现数据封装。而Bundle则是通过Map的数据结构来存储数据。
mMap = new HashMap<String, Object>();
mParcelledData
两者同时只有一个有效。
一旦unparcel以后,mParcelledData
的数据将被填充到mMap中,同时值为null。在writeToParcel和readFromParcel中则直接使用mParcelledData.此时一般通过IBinder关联两个进程的通信。
关于Bundle则是实现了Parcelable接口的类,通过上面提到的HashMap和一个Parcel来存储数据。
- public final class Bundle implements Parcelable, Cloneable {
- private static final String LOG_TAG = "Bundle";
- public static final Bundle EMPTY;
- static {
- EMPTY = new Bundle();
- EMPTY.mMap = Collections.unmodifiableMap(new HashMap<String, Object>());
- }
- // Invariant - exactly one of mMap / mParcelledData will be null
- // (except inside a call to unparcel)
- /* package */ Map<String, Object> mMap = null;
- /*
- * If mParcelledData is non-null, then mMap will be null and the
- * data are stored as a Parcel containing a Bundle. When the data
- * are unparcelled, mParcelledData willbe set to null.
- */
- /* package */ Parcel mParcelledData = null;
- public void putFloat(String key, float value) {
- unparcel();//首先解析出mParcelledData到mMap
- mMap.put(key, value);
- }
- /* package */ synchronized void unparcel() {
- if (mParcelledData == null) {
- return;
- }
- int N = mParcelledData.readInt();
- if (N < 0) {
- return;
- }
- if (mMap == null) {
- mMap = new HashMap<String, Object>();
- }
- mParcelledData.readMapInternal(mMap, N, mClassLoader);
- mParcelledData.recycle();
- mParcelledData = null;//回收以后值为null
- }
Android:Intent传递数据的几种类型和源码实现的更多相关文章
- Android Intent传递数据
刚开始看郭大神的<>,实现以下里面的一些例子.Intent传递数据. 我们利用显示的方式进行Intent的启动. 1.启动intent并输入数据. Intent intent=new In ...
- Android Intent 传递数据注意事项
不要通过 Intent 在 Android 基础组件之间传递大数据(binder transaction缓存为 1MB),可能导致 OOM.
- 【转】Android 之最新最全的Intent传递数据方法
原文地址:https://www.jianshu.com/p/1169dba99261 intent传递数据 为什么要和intent单独拿出来讲,因为Intent传递数据也是非常重要的 一.简单的传递 ...
- Android学习笔记(十二)——使用意图传递数据的几种方式
使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...
- Android 开发中使用Intent传递数据的方法
Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.shor ...
- Android学习之Intent传递数据
Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...
- Android Activity传递数据使用getIntent()接收不到,揭秘Intent传递数据与Activity启动模式singleTask的关系。
activity通过intent传递数据的时候,如果activity未启动,那么在这个刚启动的activity里通过getIntent()会获取到这个intent的数据.. 如果要启动的activit ...
- 安卓通过putExtra传递数据的几种方式
通过intent传递数据时,使用以下代码报错: hMap<string, object=""> map=(Map<string, object="&qu ...
- Intent传递对象的几种方式
原创文章.转载请注明 http://blog.csdn.net/leejizhou/article/details/51105060 李济洲的博客 Intent的使用方法相信你已经比較熟悉了,Inte ...
随机推荐
- Hand 3D Pose Estimation
https://cvarlab.icg.tugraz.at/projects/hand_detection/
- Inside Flask - flask.__init__.py 和核心组件
Inside Flask - flask.__init__.py 和核心组件 简单的示例 首先看看一个简单的示例.使用 Flask ,通常是从 flask 模块导入 Flask . request 等 ...
- Objective-C中 Self和 Super详解
Objective-C中 Self和 Super详解 Objective-C 中Self 和 Super 详解本文要介绍的内容,在 Objective-C 中的类实现中经常看到这两个关键字 self ...
- Java遇见HTML——JSP篇之JavaWeb简介
一.什么是WEB应用程序 Web应用程序是一种可以通过Web(互联网)访问的应用程序.Web应用程序的一个最大好处是用户很容易访问应用程序.用户只需要有浏览器即可,不需要再安装其他软件. 为什么要学习 ...
- 强制回收和IDisposable.Dispose方法
如果某对象的 Dispose 方法被调用一次以上,则该对象必须忽略第一次调用后的所有调用. 如果对象的 Dispose 方法被多次调用,该对象一定不要引发异常. 除Dispose 之外的实例方法在资源 ...
- git log 常用命令
1.git log 如果不带任何参数,它会列出所有历史记录,最近的排在最上方,显示提交对象的哈希值,作者.提交日期.和提交说明.如果记录过多,则按Page Up.Page Down.↓.↑来控制显示: ...
- cookie&session&servletContext
一.cookie VS session 1)应用场景 cookie可以用于: 记录用户上次登录的时间 记住用户名和密码 session可以用于: 防止非法登录(即直接跳转到本来需登录验证方可登录的页面 ...
- 查找g++文档的方法
http://www.gnu.org/ -> Software(http://www.gnu.org/software/software.html) ->搜索 "gcc" ...
- Linux启动时卡住
该系统本是oracle rac的测试环境,在删除oracle软件后重启时系统卡住(没有按照oracle官方要求删除oracle软件).如下图: 处理过程: 1.使用单用户模式登陆 先在GRUB启动菜单 ...
- Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)
控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...