在查看Bundle源码时,Bundle继承自BaseBundle,实现了Cloneable,Parcelable两个接口。

Cloneable是个空接口,只是某个类重写了Object的clone()方法时,需要实现Cloneable接口,否则在调用clone()方法时,会报CloneNotSupportedExcption异常,因此Cloneable只是合法调用的clone()的标识。关于Cloneable的使用建议,请参考 http://www.artima.com/intv/bloch13.html

要分析Parceable,先要了解Parcel,请参考

1.https://developer.android.com/reference/android/os/Parcel.html

2.http://stackoverflow.com/questions/1626667/how-to-use-parcel-in-android

3.http://prasanta-paul.blogspot.jp/2010/06/android-parcelable-example.html

在Android developer上,是这样描述Parcel的

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general Parcelable interface), and references to live IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel.
Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.
The bulk of the Parcel API revolves around reading and writing data of various types. There are six major classes of such functions available.

Parcel是个消息(data and object references)的容器,可以通过IBinder发送,IBinder是android用于进程间通信的方式(IPC)。Parcel可以携带序列化后(flattened/marshalled/serialized,通过使用多种类型的writing函数或者Parcelable接口)的数据,在IPC的另外一侧反序列化数据(变回序列化前的对象);Parcel也可以携带活动的IBinder对象,传递到Parcel中与原本IBinder相连的代理IBinder中。

Parcel不是通用序列化机制(Android特有,java的序列化机制是Serilizable,在效率上不如Parcel)。这个class(以及相应的Parcelable API,用于将用于将任意对象转换成Parcel)被设计为高性能的IPC传输方式。因此将Parcel数据放置在持久化存储位置是不合适的,任何基于Parcel中数据实现的变化都会导致旧数据不可读。

与Parcel 相关的API有多种读写不同类型的方式,主要的6种类如:

Primitives
Primitive Arrays
Parcelables
Bundles
Active Objects
Untyped Containers

示例代码片段:

假如有个对象如下,通过Intent在activity间传递

public class ParcelData{int id;String name;public ParcelData(int mId,String mName){id = mId;name = mName;}
}

如果将ParcelData放在Intent中,程序会报错,因为ParcelData没有序列化,所以要实现序列化的接口,在这里我们使用Parcelable

public class ParcelData implements Parcelable{int id;String name;public ParcelData(int mId,String mName){id = mId;name = mName;}
}

要实现Parcelable接口,程序提示覆盖两个函数,用于序列化对象

describeContents() ----描述将要进行序列化的对象的类型

writeToParcel(Parcel dest, int flags) -----真正进行序列化的位置,需要单独的序列化对象的的每个元素

@Override
public int describeContents() {return 0;
}@Override
public void writeToParcel(Parcel dest, int flags) {dest.writeInt(id);dest.writeString(name);}

describeContents()和writeToParcel(Parcel dest, int flags)是为了将对象序列化,若是得到序列化后的流,怎么反序列化成对象,需要在ParcelData中定义一个CREATOR的变量

protected ParcelData(Parcel in) {id = in.readInt();name = in.readString();
}
public static final Creator<ParcelData> CREATOR = new Creator<ParcelData>() {@Overridepublic ParcelData createFromParcel(Parcel in) {return new ParcelData(in);}@Overridepublic ParcelData[] newArray(int size) {return new ParcelData[size];     返回ParcelData类型的数组}
};

到此ParcelData已经实现,可以通过Intent在不同进程间传递。

在第一个Activity中创建序列化对象(MainActivity.java)

ArrayList<ParcelData> dataList = new ArrayList<ParcelData>();
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);ParcelData pd1 = new ParcelData(13,"beijing");
ParcelData pd2 = new ParcelData(25,"guangzhou");
dataList.add(pd1);
dataList.add(pd2);intent.putParcelableArrayListExtra("test",dataList);   将ParcelData类型的链表放置在Intent中
startActivity(intent);

在第二个Activity中反序列化(SecondActivity)

ArrayList<ParcelData> dataList = new ArrayList<ParcelData>();
dataList = getIntent().getParcelableArrayListExtra("test");    从Intent中取出ParcelData类型的对象链表

关于parcel的介绍相关推荐

  1. Android进阶——Android跨进程通讯机制之Binder、IBinder、Parcel、AIDL

    前言 Binder机制是Android系统提供的跨进程通讯机制,这篇文章开始会从Linux相关的基础概念知识开始介绍,从基础概念知识中引出Binder机制,归纳Binder机制与Linux系统的跨进程 ...

  2. Android跨进程通讯机制之Binder、IBinder、Parcel、AIDL

    https://blog.csdn.net/qq_30379689/article/details/79451596 前言 Binder机制是Android系统提供的跨进程通讯机制,这篇文章开始会从L ...

  3. 打包工具的配置教程见的多了,但它们的运行原理你知道吗?

    前端模块化成为了主流的今天,离不开各种打包工具的贡献.社区里面对于webpack,rollup以及后起之秀parcel的介绍层出不穷,对于它们各自的使用配置分析也是汗牛充栋.为了避免成为一位" ...

  4. 下一代前端打包工具-Parcel介绍

    Parcel的特性 快速打包 - 多核编译,以及文件系统缓存,即使在重新启动之后也能快速重新构建. 支持JS,CSS,HTML,文件资源等- 不需要安装任何插件. 在需要的时候自动使用Babel,Po ...

  5. parcel react_如何使用Parcel捆绑React.js应用程序

    parcel react by Michael Ozoemena 迈克尔·奥索埃梅纳(Michael Ozoemena) 如何使用Parcel捆绑React.js应用程序 (How to use Pa ...

  6. 从 webpack 到全面拥抱 Parcel #1 探索 Parcel

    最近大家都在关注一个很流行的类似 webpack 的前端构建工具 Parcel.这个库刚出来没多久(好像截至目前十几天),但是很受欢迎,看下图就知道. 所以值得一探! 官方地址:https://par ...

  7. python3.6 messagebox_Python Tkinter GUI编程入门介绍

    一.Tkinter介绍 Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口.Tkinter不是唯一的python图形编程接口,但是是其中比较流行的一个 ...

  8. Android中Parcel的分析以及使用

    简单点来说:Parcel就是一个存放读取数据的容器, android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel ...

  9. java intent bundle_Android 通过Intent使用Bundle传递对象详细介绍

    Android 通过Intent使用Bundle传递对象 Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法. 被传递的对象需要先实现序列化, ...

最新文章

  1. 基于高分辨率的单目深度估计网络(AAAI2021)
  2. device not ready cuda
  3. 关于.NET技术体系的思维导图
  4. Oralce 使用SQL中的exists 和not exists 用法详解
  5. 苹果7信号天线内部位置_新专利显示未来Apple Watch Wi-Fi及无线天线或将嵌入显示屏中...
  6. 【Time系列一】datetime的妙用
  7. 电脑如何测网速_物联网卡的网速到底怎么样呢
  8. opencv论坛_Opencv批量添加logo的解决方案
  9. E: Sub-process /usr/bin/dpkg returned an error code (1)
  10. jsp:setProperty的用法
  11. 笔记本辐射与日常电器辐射对比
  12. element-plus小demo
  13. 在微信朋友圈冲浪必备的心灵免疫力
  14. mac charles网页代理https使用笔记
  15. canvas基本使用,以及七巧板绘制
  16. 用数字万用表测量三极管的方法
  17. vue element upload组件 file-list的动态绑定
  18. 学计算机的演员,南开大学计算机系到演员 张桐回顾“不安分”的青春_TOM明星...
  19. 基于JAVA酒店信息管理计算机毕业设计源码+数据库+lw文档+系统+部署
  20. 介绍几个在线画流程图的工具

热门文章

  1. 拿了北京户口!却是跌落的开始....
  2. USB 电池充电规范
  3. 打印后台程序没有运行,怎么办?
  4. ubuntu conf误删除的问题
  5. matplotlib给某一个点添加注释
  6. neutron组网规划(flat、vlan类型)
  7. chunk和block 区别
  8. Revit“原点”、“中心”、“测量点”在哪里?
  9. Shell 编程 ~ 从入门到入坑。
  10. 基尔霍夫电压定律解析