android序列化Serializable、Parcelable(一)
2015-09-08 17:28
489 查看
1.是什么
什么是序列化?
序列化是将对象转化成二进制的数据,以便于进行传输,存储。比如两个activity间可以用intent传输序列化的对象。Serializable和Parcelable都是用于序列化的接口。
* Serializable 是java提供的,里面空空如也,主要是一个标识,表示该对象可序列化
* Parcelable 是android提供的,里面有好多东西,如下:
2.为什么
前文也提到,序列化可以进行传输和存储,基本有:
—— 当你想把的内存中的对象保存到一个文件中或者数据库中
—— 当你想在网络上传送对象
—— 当你想通过远程方法调用对象的时候;
3.怎么用
通过实现Serializable或Parcelable接口就能使对象序列化,Serializable低效,简单、Parcelable相反。
1.Serializable
2.Parcelable
3. intent 传输
注:
a)当一个父类实现序列化,子类自动实现序列化,不需要显式实现Serializable接口;
b)当一个对象的实例变量引用其他对象,序列化该对象时也把引用对象进行序列化;
c) static,transient后的变量不能被序列化;
什么是序列化?
序列化是将对象转化成二进制的数据,以便于进行传输,存储。比如两个activity间可以用intent传输序列化的对象。Serializable和Parcelable都是用于序列化的接口。
* Serializable 是java提供的,里面空空如也,主要是一个标识,表示该对象可序列化
* Parcelable 是android提供的,里面有好多东西,如下:
public interface Parcelable { /** * Flag for use with {@link #writeToParcel}: the object being written * is a return value, that is the result of a function such as * "<code>Parcelable someFunction()</code>", * "<code>void someFunction(out Parcelable)</code>", or * "<code>void someFunction(inout Parcelable)</code>". Some implementations * may want to release resources at this point. */ public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001; /** * Bit masks for use with {@link #describeContents}: each bit represents a * kind of object considered to have potential special significance when * marshalled. */ public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001; /** * Describe the kinds of special objects contained in this Parcelable's * marshalled representation. * * @return a bitmask indicating the set of special object types marshalled * by the Parcelable. */ public int describeContents(); /** * Flatten this object in to a Parcel. * * @param dest The Parcel in which the object should be written. * @param flags Additional flags about how the object should be written. * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}. */ public void writeToParcel(Parcel dest, int flags); /** * Interface that must be implemented and provided as a public CREATOR * field that generates instances of your Parcelable class from a Parcel. */ public interface Creator<T> { /** * Create a new instance of the Parcelable class, instantiating it * from the given Parcel whose data had previously been written by * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}. * * @param source The Parcel to read the object's data from. * @return Returns a new instance of the Parcelable class. */ public T createFromParcel(Parcel source); /** * Create a new array of the Parcelable class. * * @param size Size of the array. * @return Returns an array of the Parcelable class, with every entry * initialized to null. */ public T[] newArray(int size); } /** * Specialization of {@link Creator} that allows you to receive the * ClassLoader the object is being created in. */ public interface ClassLoaderCreator<T> extends Creator<T> { /** * Create a new instance of the Parcelable class, instantiating it * from the given Parcel whose data had previously been written by * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and * using the given ClassLoader. * * @param source The Parcel to read the object's data from. * @param loader The ClassLoader that this object is being created in. * @return Returns a new instance of the Parcelable class. */ public T createFromParcel(Parcel source, ClassLoader loader); } }
2.为什么
前文也提到,序列化可以进行传输和存储,基本有:
—— 当你想把的内存中的对象保存到一个文件中或者数据库中
—— 当你想在网络上传送对象
—— 当你想通过远程方法调用对象的时候;
3.怎么用
通过实现Serializable或Parcelable接口就能使对象序列化,Serializable低效,简单、Parcelable相反。
1.Serializable
public class MyClass implements Serializable { private static final long serialVersionUID = 1L; // 定义序列化ID private String a = null; private int b = 0; }
2.Parcelable
public class MyClass implements Parcelable { private String a = null; private int b = 0; private MyClassA myClassA = new MyClassA; private List<MyClassB> myClassB = new ArrayList<MyClassB>(); public MyClass(Parcel parcel) { // 按变量定义的顺序读取 a = parcel.readString(); b = parcel.readInt(); myClassA = parcel.readParcelable(MyClassA.class.getClassLoader()); Parcelable[] pars = parcel.readParcelableArray(MyClassB.class.getClassLoader()); myClassB = Arrays.asList(Arrays.asList(pars).toArray(new MyClassB[pars.length])); } public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { // 按变量定义的顺序写入 dest.writeString(a); dest.writeString(b); dest.writeParcelable(myClassA, flags); dest.writeParcelableArray((myClassB.toArray(new MyClassB[myClassB.size()])), flags); } public static final Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>() { public Param createFromParcel(Parcel source) { return new MyClass(source); } public MyClass[] newArray(int size) { return new MyClass[size]; } }; }
3. intent 传输
<pre name="code" class="java">Intent intent = new Intent(this, Activity2.class); Bundle bundle = new Bundle(); bundle.putSerializable("key", class1);
注:
a)当一个父类实现序列化,子类自动实现序列化,不需要显式实现Serializable接口;
b)当一个对象的实例变量引用其他对象,序列化该对象时也把引用对象进行序列化;
c) static,transient后的变量不能被序列化;
bundle.putParcelable("key", class1);
相关文章推荐
- Android 动态添加每条输入的内容
- Android下拉刷新SwipeRefreshLayout
- Android笔记:20150823
- Android笔记:20150824(this关键字的使用)
- Android笔记:20150820
- 【第四篇章-android平台MediaCodec】根据编码类型MIME_TYPE获取MediaCodecInfo
- Android笔记:20150408
- Android笔记:20150414
- Android客户端写Cookie和内嵌的网页实现登录状态的同步
- Android笔记:20150406
- 基于xmpp openfire smack开发之Android消息推送技术原理分析和实践[4]
- Android笔记(二十四)广播机制
- Android笔记:20150308
- GitHub 优秀的 Android 开源项目
- Android基础之CursorAdapter 的用法与获取联系人信息显示、并点击拨号
- android开发记录
- 根据路径获取图片,缩放图片
- Android Launcher分析和修改5——HotSeat分析
- Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
- Android中的权限管理(基于Permission ProtectionLevel)