android序列化(2)Parcelable与Parcel
1.简介
Parcel : 包裹
Android采用这个它封装消息数据。这个是通过IBinder通信的消息的载体。需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等)。
Parcelable: 可放入包裹的协议接口
如果需要封装到包裹中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。
2.示例
1 import android.content.Intent; 2 import android.os.Parcel; 3 import android.os.Parcelable; 4 5 import com.e.weixin.R; 6 import com.e.weixin.contacts.beans.ContactBean; 7 8 public class MsgBean implements Parcelable{ 9 10 public static final int MSB_TYPE_TXT = 0; 11 public static final int MSB_TYPE_TP = 1; 12 public static final int MSB_TYPE_XSP = 2; 13 public static final int MSB_TYPE_HB = 3; 14 public static final int MSB_TYPE_ZZ = 4; 15 public static final int MSB_TYPE_SC = 5; 16 public static final int MSB_TYPE_WZ = 6; 17 public static final int MSB_TYPE_SPLT = 7; 18 public static final int MSB_TYPE_MP = 8; 19 public static final int MSB_TYPE_YY = 9; 20 21 22 ContactBean contact; 23 public int type ; 24 public String title; 25 public String content; 26 public int icon; 27 public int movie_res; 28 29 30 31 32 //================== 33 public MsgBean(){ 34 35 } 36 37 //下面代码都是as自动生成 38 protected MsgBean(Parcel in) { 39 //从Parcel中读一个实现了Parcelable接口的自定义的类,需要ClassLoader 40 contact = in.readParcelable(ContactBean.class.getClassLoader()); 41 type = in.readInt(); 42 title = in.readString(); 43 content = in.readString(); 44 icon = in.readInt(); 45 movie_res = in.readInt(); 46 } 47 48 public static final Creator<MsgBean> CREATOR = new Creator<MsgBean>() { 49 @Override 50 public MsgBean createFromParcel(Parcel in) { 51 return new MsgBean(in); 52 } 53 54 @Override 55 public MsgBean[] newArray(int size) { 56 return new MsgBean[size]; 57 } 58 }; 59 60 @Override 61 public int describeContents() { 62 return 0; 63 } 64 65 @Override 66 public void writeToParcel(Parcel dest, int flags) { 67 dest.writeParcelable(contact,flags);//写另一个自定义的类,它也实现了Parcelable接口 68 dest.writeInt(type); 69 dest.writeString(title); 70 dest.writeString(content); 71 dest.writeInt(icon); 72 dest.writeInt(movie_res); 73 } 74 75 public static void test(){ 76 77 int sz = -1,position = -1; 78 //1,准备一个Parcel,其它数据放入其中 79 Parcel parcel = Parcel.obtain(); 80 sz = parcel.dataSize(); 81 position = parcel.dataPosition(); 82 83 //2,准备要写入的对象,其中包含另一个实现了Parcelable的对象 84 ContactBean cb = new ContactBean(); 85 cb.name = "name"; 86 cb.sectionCount = 3; 87 cb.isSection = true; 88 cb.icon = R.drawable.ebf; 89 90 MsgBean mb1 = new MsgBean(); 91 mb1.type = 1; 92 mb1.icon = R.drawable.ebf; 93 mb1.content = "content"; 94 mb1.title = "title"; 95 mb1.contact = cb; 96 mb1.movie_res = R.raw.movie; 97 98 //3,写入 99 mb1.writeToParcel(parcel,1); 100 sz = parcel.dataSize(); 101 position = parcel.dataPosition(); 102 103 //4,读取 104 MsgBean mb2 = MsgBean.CREATOR.createFromParcel(parcel); 105 106 MsgBean mb3 = parcel.readParcelable(MsgBean.class.getClassLoader()); 107 108 parcel.writeParcelable(mb1, 0); 109 110 MsgBean mb4 = MsgBean.CREATOR.createFromParcel(parcel); 111 MsgBean mb5 = parcel.readParcelable(MsgBean.class.getClassLoader()); 112 113 114 //5,开始序列 115 Intent intent = new Intent(); 116 intent.putExtra("parcelable",mb1); 117 intent.putExtra("int",100); 118 119 //6,开始反序列 120 //通常是在另一个地方得到上面的Intent 121 MsgBean mb6 = intent.getParcelableExtra("parcelable"); 122 int data = intent.getIntExtra("int",-1); 123 } 124 }