您的位置:首页 > 移动开发 > Android开发

android序列化Serializable、Parcelable(一)

2015-09-08 17:28 489 查看
1.是什么

什么是序列化?

序列化是将对象转化成二进制的数据,以便于进行传输,存储。比如两个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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: