一、什么是序列化
当我们需要在 Intent 上或者在进程间传递一个对象的时候,当我们需要将对象持久化,例如将对象保存到存储设备或者通过网络传递到其他设备的时候,我们就需要将这个对象序列化。
二、Parcelable 接口和 Serializable 接口
在Android中想要实现序列化有两种方式:
方式一:实现由Java提供的 Serializable 接口
这种方式代码量少,使用方便,但是开销很大,涉及大量 I/O 操作。适合将对象序列化到存储设备或是通过网络传输的场景。
方式二:实现由 Android SDK 提供的 Parcelable 接口
这种方式使用稍微复杂点,但是效率高,是 Android 推荐的序列化方式。适合在内存序列化的场景,如在 Intent 上传递对象数据或是在进程间通信传递对象。
三、Parcelable 接口定义
public interface Parcelable {
public int describeContents();
public void writeToParcel(Parcel dest, int flags);
public interface Creator<T> {
public T createFromParcel(Parcel in);
public T[] newArray(int size);
}
1.describeContents方法
描述内容,基本都是默认返回 0 就可以,官方文档上写了如果对象在 writeToParcel 方法的输出中包含一个文件描述符,则需要返回 Parcelable 的 CONTENTS_FILE_DESCRIPTOR,即 1。
2.writeToParcel(Parcel dest, int flags)方法
将这个对象的数据写到 Parcel 中,即参数中的 dest;flags 参数有两种值:0或1,按《Android开发艺术探索》书上写的是,1表示当前对象需要作为返回值返回,不能立即释放资源,几乎所有情况都为0,不过本人不是很理解这句话的意思,若有哪位同学清楚,还望告知。
3.静态的 Parcelable.Creator 接口,包含两个方法:
createFromParcel(Parcel in) 方法,从序列化后的对象 (in) 中创建原始对象;
newArray(int size) 方法,创建指定长度的原始对象数组,只需简单地 return new T[size] 即可。
四、代码示例
public class User implements Parcelable {
private int userId;
private String userName;
private boolean isMan;
protected User(int userId, String userName, boolean isMan) {
this.userId = userId;
this.userName = userName;
this.isMan = isMan;
}
protected User(Parcel in) {
userId = in.readInt();
userName = in.readString();
isMan = in.readByte() != 0;
}
public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int size) {
return new User[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(userId);
dest.writeString(userName);
dest.writeByte((byte) (isMan ? 1 : 0));
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public boolean isMan() {
return isMan;
}
public void setMan(boolean man) {
isMan = man;
}
}
虽说实现 Parcelable 接口相对复杂,但是 AS 会帮我们完成大部分事情。我们先写好这个类的成员变量,再继承 Parcelable 接口,这时 AS 会自动帮我们重写 describeContents 和 writeToParcel 这两个方法,以及实现 Creator 接口。而我们只需要添加构造方法及 get/set 方法。
如果我们要自己手动写,或者是后面成员变量需要修改,则要注意两点:
1. 在 writeToParcel 方法中将变量序列化的过程,和在 createFromParcel方法中反序列化获取数据的过程,都必须和类中变量声明的顺序一样,否则会出错;
2. boolean 类型的变量需用上面代码示例中的这种方式:
dest.writeByte((byte) (isMan ? 1 : 0));
isMan = in.readByte() != 0;
接下来将 User 对象通过 Intent 传递
user = new User(123, "parcelUser", false);
Intent i = new Intent(ParcelableActivity.this, ParcelableReceiveActivity.class);
i.putExtra("user", user);
startActivity(i);
这里也可以先将 User 对象放入 Bundle 中,再通过 Intent 传递。
最后是接收
User user = getIntent().getParcelableExtra("user");
mTextview.setText(user.getUserName() + " " + user.getUserId() + " " + user.isMan());