首先定义字段,我这里定义成这样
// 消息ID
protected int msgId;
// 消息内容
protected String msgContent;
// 消息用户名
protected String msgUserName;
// 消息发送状态
protected int msgState;
// 消息发送者的IP
protected String msgMyIp;
// 对方的IP
protected String msgToIp;
// 消息发送方向
protected int msgDirection;
// 消息发送附带
protected String msgMsgData;
// 用户自定义消息
protected String msgUserData;
继承Parcelable接口,实现其中的两个方法describeContents()和writeToParcel()代码如下
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.msgId);
dest.writeString(this.msgContent);
dest.writeString(this.msgUserName);
dest.writeInt(this.msgState);
dest.writeString(this.msgMyIp);
dest.writeString(this.msgToIp);
dest.writeInt(this.msgDirection);
dest.writeString(this.msgMsgData);
dest.writeString(this.msgUserData);
}
其次写一个私有的全参数构造函数和一个共有的无参构造,当然,自己想去再写其他的构造也可以
private Message(Parcel source) {
this.msgId = source.readInt();
this.msgContent = source.readString();
this.msgUserName = source.readString();
this.msgState = source.readInt();
this.msgMyIp = source.readString();
this.msgToIp = source.readString();
this.msgDirection = source.readInt();
this.msgMsgData = source.readString();
this.msgUserData = source.readString();
}
public Message() {
}
最后一步,手写下面这个方法,方法名必须是CREATOR
public static final Parcelable.Creator<Message> CREATOR = new Creator<Message>() {
@Override
public Message[] newArray(int size) {
return new Message[size];
}
@Override
public Message createFromParcel(Parcel source) {
return new Message(source);
}
};
这样就实现了序列化,完整代码如下:
public class Message implements Parcelable {
// 消息ID
protected int msgId;
// 消息内容
protected String msgContent;
// 消息用户名
protected String msgUserName;
// 消息发送状态
protected int msgState;
// 消息发送者的IP
protected String msgMyIp;
// 对方的IP
protected String msgToIp;
// 消息发送方向
protected int msgDirection;
// 消息发送附带
protected String msgMsgData;
// 用户自定义消息
protected String msgUserData;
private Message(Parcel source) {
this.msgId = source.readInt();
this.msgContent = source.readString();
this.msgUserName = source.readString();
this.msgState = source.readInt();
this.msgMyIp = source.readString();
this.msgToIp = source.readString();
this.msgDirection = source.readInt();
this.msgMsgData = source.readString();
this.msgUserData = source.readString();
}
public Message() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.msgId);
dest.writeString(this.msgContent);
dest.writeString(this.msgUserName);
dest.writeInt(this.msgState);
dest.writeString(this.msgMyIp);
dest.writeString(this.msgToIp);
dest.writeInt(this.msgDirection);
dest.writeString(this.msgMsgData);
dest.writeString(this.msgUserData);
}
public static final Parcelable.Creator<Message> CREATOR = new Creator<Message>() {
@Override
public Message[] newArray(int size) {
return new Message[size];
}
@Override
public Message createFromParcel(Parcel source) {
return new Message(source);
}
};
<span style="color:#FF0000;">//get set 略</span>
}