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;


继承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>

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值