android序列化Serializable、Parcelable(一)

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值