標籤:
首先,我們要知道Android序列化的方法有兩種,一個是實現Serializable,這是JavaSE中就支援的介面,後來google推出了專門針對Android的介面Parcelable(其效能相對Serializable將近快了10倍)
然後我們要知道Android內部傳遞執行個體的基本方法:一是Bundle.putSerializable(Key,Object),另一種是Bundle.putParcelable(Key,Object),它們分別要實現Serializable和Parcelable介面。需要注意的一點是Parcelable不能使用在要將資料存放區在磁碟上的情況,因為Parcelable不能很好的保證資料的持久性在外界有變化的情況下。這種情況還是使用Serializable比較好。
我們先看Parcelable介面的定義:
1 public interface Parcelable 2 { 3 //內容描述介面,基本不用管 4 public int describeContents(); 5 //寫入介面函數,打包 6 public void writeToParcel(Parcel dest, int flags); 7 //讀取介面,目的是要從Parcel中構造一個實現了Parcelable的類的執行個體處理。因為實作類別在這裡還是不可知的,所以需要用到模板的方式,繼承類名通過模板參數傳入 8 //為了能夠實現模板參數的傳入,這裡定義Creator嵌入介面,內含兩個介面函數分別返回單個和多個繼承類執行個體 9 public interface Creator<T> 10 {11 public T createFromParcel(Parcel source);12 public T[] newArray(int size);13 }14 public interface ClassLoaderCreator<T> extends Creator<T>15 {16 public T createFromParcel(Parcel source, ClassLoader loader);17 }18 }
繼承該介面後,我們主要是重寫writeToParcel()將資料序列化成一個Parcel對象
下面是將一個User類實現了Parcelable的具體源碼
1 package com.catvideo.tv.catvideo.com.catvideo.user; 2 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 6 /** 7 * @author lcc [email protected] 8 * @version 2016??5??31?? ????12:09:20 9 * @Description 10 */ 11 public class User implements Parcelable { 12 int userId; 13 private String userName; 14 private String passWord; 15 private String email; 16 boolean state; 17 18 public User() { 19 20 } 21 22 public User(String email, String passWord) { 23 this.email = email; 24 this.passWord = passWord; 25 } 26 27 public User(int userId, String userName, String passWord, boolean state) { 28 this.userId = userId; 29 this.userName = userName; 30 this.passWord = passWord; 31 this.state = state; 32 } 33 34 public User(int userId, String userName, String passWord) { 35 this.userId = userId; 36 this.userName = userName; 37 this.passWord = passWord; 38 this.state = false; 39 } 40 41 public int getUserId() { 42 return userId; 43 } 44 45 public void setUserId(int userId) { 46 this.userId = userId; 47 } 48 49 public String getUserName() { 50 return userName; 51 } 52 53 public void setUserName(String userName) { 54 this.userName = userName; 55 } 56 57 public String getPassWord() { 58 return passWord; 59 } 60 61 public void setPassWord(String passWord) { 62 this.passWord = passWord; 63 } 64 65 66 public String getEmail() { 67 return email; 68 } 69 70 public void setEmail(String email) { 71 this.email = email; 72 } 73 74 public boolean isState() { 75 return state; 76 } 77 78 public void setState(boolean state) { 79 this.state = state; 80 } 81 82 @Override 83 public int describeContents() { 84 return 0; 85 } 86 87 @Override 88 public void writeToParcel(Parcel dest, int flags) { 89 boolean[] states = {state}; 90 dest.writeString(userName); 91 dest.writeInt(getUserId()); 92 dest.writeBooleanArray(states); 93 } 94 95 // public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { 96 ////重寫Creator 97 // 98 // @Override 99 // public User createFromParcel(Parcel source) {100 // User user = new User();101 //// user.userName=source.readString()102 //103 //// p.map = source.readHashMap(HashMap.class.getClassLoader());104 //// p.name = source.readString();105 // return p;106 // }107 //108 // @Override109 // public User[] newArray(int size) {110 // // TODO Auto-generated method stub111 // return null;112 // }113 // };114 }
之後可以這樣使用:
intent.putExtra("user", user);
或者使用Bundle.putParcelable(Key,Object)
Serializable實現與Parcelabel實現的區別
1)Serializable的實現,只需要implements Serializable 即可,不需要實現任何方法。這隻是給對象打了一個標記,系統會自動將其序列化。
2)Parcelabel的實現,不僅需要implements Parcelabel,還需要在類中添加一個靜態成員變數CREATOR,這個變數需要實現 Parcelable.Creator 介面。
Android Parcelable介面的使用/序列化資料