1、概念
Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface。
android提供了一种新的类型:parcel(中文解释:包裹,小包),本类用来封装数据的容器,封装后的数据可以通过Intent或IPC传递,除了基本类型外,只有实现了Parcelable接口的类才能放入parcel中。parcel一般都用在Binder通信,通过read和write方法进行客户端与服务端的数据传递(通信)。
比如:frameworks层服务端与hardware客户端的Binder通信
reply->writeInt32(getCardReaderSize());
int mid = data.readInt32();
用来存放parcel数据的是内存(RAM),而不是永久介质(Nand等)。
parcelable定义了把数据写入parcel和从parcel读出数据的接口,一个类的实例,如果需要封装到消息中去,就必须实现这一接口,如果实现了这个接口,该类的实例就是可以“被打包”。
2、为什么要实现Parcelable接口?
实现Parcelable就是为了进行序列化,那么,为什么要序列化?
- 永久性保存对象,保存对象的字节序列到本地文件中;
- 通过序列化对象在网络中传递对象;
- 通过序列化在进程间传递对象。
3、实现序列化的方法
Android中实现序列化有两个选择:一是实现Serializable接口(是JavaSE本身就支持的),一是实现Parcelable接口(是Android特有功能,效率比实现Serializable接口高效,可用于Intent数据传递,也可以用于进程间通信(IPC))。实现Serializable接口非常简单,声明一下就可以了,而实现Parcelable接口稍微复杂一些,但效率更高,推荐用这种方法提高性能。
注:Android中Intent传递对象有两种方法:一是Bundle.putSerializable(Key,Object),另一种是Bundle.putParcelable(Key,Object)。当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口。
4、选择序列化方法的原则
- 在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable;
- Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC;
- Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的持续性在外界有变化的情况下。尽管Serializable效率低点,但此时还是建议使用Serializable 。
5、应用场景
需要在多个部件(Activity或Service)之间通过Intent传递一些数据,简单类型(如:数字、字符串)的可以直接放入Intent。复杂类型必须实现Parcelable接口。
6、Parcelable接口定义
public interface Parcelable
{
//内容描述接口,可以不做任何操作,直接return 0即可;
public int describeContents();
//写入接口函数,打包
public void writeToParcel(Parcel dest, int flags);
//读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入
//为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例
public interface Creator<T>
{
public T createFromParcel(Parcel source);
public T[] newArray(int size);
}
}
7、实现Parcelable步骤
- implements Parcelable;
- 重写describeContents方法,内容接口描述,默认返回0就可以;
- 重写writeToParcel方法,将你的对象序列化为一个Parcel对象,即:将类的数据写入外部提供的Parcel中,打包需要传递的数 据到Parcel容器保存,以便从 Parcel容器获取数据;
- 实例化静态内部对象CREATOR实现接口Parcelable.Creator:
public static final Parcelable.Creator<T> CREATOR
注:其中public static final一个都不能少,内部对象CREATOR的名称也不能改变,必须全部大写。需重写本接口中的两个方法:createFromParcel(Parcel in) 实现从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层,newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话即可(return new T[size]),供外部类反序列化本类数组使用。
总之:通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面,在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的顺序和读的顺序必须一致。
8、Serializable实现与Parcelabel实现的区别
- Serializable的实现,只需要implements Serializable 即可。这只是给对象打了一个标记,系统会自动将其序列化。
- Parcelabel的实现,不仅需要implements Parcelabel,还需要在类中添加一个静态成员变量CREATOR,这个变量需要实现 Parcelable.Creator 接口。
9、实现Parcelable接口代码
import java.util.List;
import android.os.Parcel;
import android.os.Parcelable;
public class ContentNavInfoBean implements Parcelable{
private String navId;
private String navName;
private String navType;
private String orderNum;
private String lessonNum;
public String getNavId() {
return navId;
}
public void setNavId(String navId) {
this.navId = navId;
}
public String getNavName() {
return navName;
}
public void setNavName(String navName) {
this.navName = navName;
}
public String getNavType() {
return navType;
}
public void setNavType(String navType) {
this.navType = navType;
}
public String getOrderNum() {
return orderNum;
}
public void setOrderNum(String orderNum) {
this.orderNum = orderNum;
}
public String getLessonNum() {
return lessonNum;
}
public void setLessonNum(String lessonNum) {
this.lessonNum = lessonNum;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(navId);
dest.writeString(navName);
dest.writeString(navType);
dest.writeString(orderNum);
dest.writeString(lessonNum);
}
public static final Parcelable.Creator<ContentNavInfoBean> CREATOR = new Parcelable.Creator<ContentNavInfoBean>()
{
public ContentNavInfoBean createFromParcel(Parcel in)
{
ContentNavInfoBean infoBean = new ContentNavInfoBean();
infoBean.navId=in.readString();
infoBean.navName=in.readString();
infoBean.navType=in.readString();
infoBean.orderNum=in.readString();
infoBean.lessonNum=in.readString();
return infoBean;
}
public ContentNavInfoBean[] newArray(int size)
{
return new ContentNavInfoBean[size];
}
};
}
10、Intent参数传递序列化对象
Intent intent = new Intent(A.class,B.class);
Bundle bundle = new Bundle();
bundle.putParcelable("contentNavInfo", contentNavInfo);
intent.putExtras(bundle);