一、Serializable
public class Student implements Serializable {
private static final long serialVersionUID=1L;
}
二、Parcelable
import android.os.Parcel;
import android.os.Parcelable;
/**
* <pre>
* author : 楊麗金
* time : 2018/08/28
* desc :
* </pre>
*/
public class User implements Parcelable {
public int userId;
public String userName;
public boolean isMale;
public Book mBook;
public User(int userId, String userName, boolean isMale) {
this.userId=userId;
this.userName=userName;
this.isMale=isMale;
}
/**
* 返回當(dāng)前對象的內(nèi)容描述。如果含有文件描述符:返回1,否則0【幾乎所有的情況都返回0】
* @return
*/
@Override
public int describeContents() {
return 0;
}
/**
* 將當(dāng)前對象寫入序列化結(jié)構(gòu)中
* @param dest
* @param flags:0或者1,為1時標(biāo)志當(dāng)前對象需要作為返回值返回,不能立即釋放資源【幾乎所有的情況都返回0】
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(userId);
dest.writeString(userName);
/*記下isMale ,因為是Boolean類型,
但是沒有writeBoolean,只有writeBooleanArray,
所以我們用writeInt()來記錄,1是true,0是false。
額外說下writeBooleanArray內(nèi)部其實還是用writeInt來記錄的。
*/
dest.writeInt(isMale?1:0);
dest.writeParcelable(mBook,0);
}
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>(){
/**
* 從序列化后的結(jié)構(gòu)中創(chuàng)建原始對象
* @param source
* @return
*/
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
/**
* 創(chuàng)建指定長度的原始數(shù)組
* @param size
* @return
*/
@Override
public User[] newArray(int size) {
return new User[size];
}
};
/**
* 從序列化后的結(jié)構(gòu)中創(chuàng)建原始對象
* @param source
*/
public User(Parcel source){
userId=source.readInt();
userName=source.readString();
isMale = source.readInt() == 1;
/*
由于book是另一個可序列化對象,所以它的反序列化過程需要傳遞當(dāng)前線程的上下文類加載器,
否則會報無法找到類的錯誤
*/
mBook = source.readParcelable(Thread.currentThread().getContextClassLoader());
}
}
- 我們要把一個對象序列化之前,肯定要創(chuàng)建對象吧,然后把相應(yīng)的內(nèi)容賦給對象吧,所以這里提供一個帶參數(shù)的構(gòu)造函數(shù)
public User(int userId, String userName, boolean isMale) {
this.userId=userId;
this.userName=userName;
this.isMale=isMale;
}
這樣我們寫代碼的時候就new User(10,"dyp",true)
(當(dāng)然你也可以寫setXXX方法去設(shè)置)
- 要序列化了:我們把對象里的屬性都寫到Parcel中。要按順序?qū)懭耄葧哼€要按順序讀出來。
/**
* 將當(dāng)前對象寫入序列化結(jié)構(gòu)中
* @param dest
* @param flags:0或者1,為1時標(biāo)志當(dāng)前對象需要作為返回值返回,不能立即釋放資源【幾乎所有的情況都返回0】
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(userId);
dest.writeString(userName);
/*記下isMale ,因為是Boolean類型,
但是沒有writeBoolean,只有writeBooleanArray,
所以我們用writeInt()來記錄,1是true,0是false。
額外說下writeBooleanArray內(nèi)部其實還是用writeInt來記錄的。
*/
dest.writeInt(isMale?1:0);
dest.writeParcelable(mBook,0);
}
- 序列化后的數(shù)據(jù)傳到其他進(jìn)程后,肯定要從Parcel中把數(shù)據(jù)還原回來。肯定是首先要創(chuàng)建一個User對象,然后把第二步中的值給它重新賦值。
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>(){
/**
* 從序列化后的結(jié)構(gòu)中創(chuàng)建原始對象
* @param source
* @return
*/
@Override
public User createFromParcel(Parcel source) {
return new User(source);
}
/**
* 創(chuàng)建指定長度的原始數(shù)組
* @param size
* @return
*/
@Override
public User[] newArray(int size) {
return new User[size];
}
};
/**
* 從序列化后的結(jié)構(gòu)中創(chuàng)建原始對象
* @param source
*/
public User(Parcel source){
userId=source.readInt();
userName=source.readString();
isMale = source.readInt() == 1;
/*
由于book是另一個可序列化對象,所以它的反序列化過程需要傳遞當(dāng)前線程的上下文類加載器,
否則會報無法找到類的錯誤
*/
mBook = source.readParcelable(Thread.currentThread().getContextClassLoader());
}
三、二者對比
區(qū)別
參考文獻(xiàn)
Android技能樹 — 多進(jìn)程相關(guān)小結(jié)
任玉剛_Android開發(fā)藝術(shù)探索
校招指南
code小生_Android 序列化總結(jié)