android序列化(2)Parcelable与Parcel

1.简介

Parcel  :  包裹

  Android采用这个它封装消息数据。这个是通过IBinder通信的消息的载体。需要明确的是Parcel用来存放数据的是内存(RAM),而不是永久性介质(Nand等)。

Parcelable: 可放入包裹的协议接口

  如果需要封装到包裹中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。

2.示例

 import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable; import com.e.weixin.R;
import com.e.weixin.contacts.beans.ContactBean; public class MsgBean implements Parcelable{ public static final int MSB_TYPE_TXT = ;
public static final int MSB_TYPE_TP = ;
public static final int MSB_TYPE_XSP = ;
public static final int MSB_TYPE_HB = ;
public static final int MSB_TYPE_ZZ = ;
public static final int MSB_TYPE_SC = ;
public static final int MSB_TYPE_WZ = ;
public static final int MSB_TYPE_SPLT = ;
public static final int MSB_TYPE_MP = ;
public static final int MSB_TYPE_YY = ; ContactBean contact;
public int type ;
public String title;
public String content;
public int icon;
public int movie_res; //==================
public MsgBean(){ } //下面代码都是as自动生成
protected MsgBean(Parcel in) {
//从Parcel中读一个实现了Parcelable接口的自定义的类,需要ClassLoader
contact = in.readParcelable(ContactBean.class.getClassLoader());
type = in.readInt();
title = in.readString();
content = in.readString();
icon = in.readInt();
movie_res = in.readInt();
} public static final Creator<MsgBean> CREATOR = new Creator<MsgBean>() {
@Override
public MsgBean createFromParcel(Parcel in) {
return new MsgBean(in);
} @Override
public MsgBean[] newArray(int size) {
return new MsgBean[size];
}
}; @Override
public int describeContents() {
return ;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(contact,flags);//写另一个自定义的类,它也实现了Parcelable接口
dest.writeInt(type);
dest.writeString(title);
dest.writeString(content);
dest.writeInt(icon);
dest.writeInt(movie_res);
} public static void test(){ int sz = -,position = -;
//1,准备一个Parcel,其它数据放入其中
Parcel parcel = Parcel.obtain();
sz = parcel.dataSize();
position = parcel.dataPosition(); //2,准备要写入的对象,其中包含另一个实现了Parcelable的对象
ContactBean cb = new ContactBean();
cb.name = "name";
cb.sectionCount = ;
cb.isSection = true;
cb.icon = R.drawable.ebf; MsgBean mb1 = new MsgBean();
mb1.type = ;
mb1.icon = R.drawable.ebf;
mb1.content = "content";
mb1.title = "title";
mb1.contact = cb;
mb1.movie_res = R.raw.movie; //3,写入
mb1.writeToParcel(parcel,);
sz = parcel.dataSize();
position = parcel.dataPosition(); //4,读取
MsgBean mb2 = MsgBean.CREATOR.createFromParcel(parcel); MsgBean mb3 = parcel.readParcelable(MsgBean.class.getClassLoader()); parcel.writeParcelable(mb1, ); MsgBean mb4 = MsgBean.CREATOR.createFromParcel(parcel);
MsgBean mb5 = parcel.readParcelable(MsgBean.class.getClassLoader()); //5,开始序列
Intent intent = new Intent();
intent.putExtra("parcelable",mb1);
intent.putExtra("int",); //6,开始反序列
//通常是在另一个地方得到上面的Intent
MsgBean mb6 = intent.getParcelableExtra("parcelable");
int data = intent.getIntExtra("int",-);
}
}
上一篇:浅谈Android中Serializable和Parcelable使用区别


下一篇:Android中Intent传递对象的两种方法(Serializable,Parcelable)