实体类实现Parcelable接口详解

什么是Parcelable ?
Parcelable,定义了将数据写入Parcel,和从Parcel中读出的接口。一个实体(用类来表示),如果需要封装到消息中去,就必须实现这一接口,实现了这一接口,该实体就成为“可打包的”了。
Parcelable 传递对象
Android序列化对象主要有两种方法:
1.实现Serializable接口,实现Serializable接口是JavaSE本身就支持的;
2.实现Parcelable接口,Parcelable是Android特有的功能,效率比实现Serializable接口高,像用于Intent数据传递也都支持,而且还可以用在进程间通信(IPC),
除了基本类型外,只有实现了Parcelable接口的类才能被放入Parcel中。
Parcelable接口定义
public interface Parcelable {
    //内容描述接口,基本不用管
    public int describeContents();
    //写入接口函数,打包
    public void writeToParcel(Parcel dest, int flags);
     //读取接口,目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。因为实现类在这里还是不可知的,所以需要用到模板的方式,继承类名通过模板参数传入。
    //为了能够实现模板参数的传入,这里定义Creator嵌入接口,内含两个接口函数分别返回单个和多个继承类实例。
    public interface Creator {
           public T createFromParcel(Parcel source);
           public T[] newArray(int size);
       }


实现Parcelable接口?
从parcelable接口定义中,我们可以看到,实现parcelable接口,需要我们实现下面几个方法:
1.describeContents方法。内容接口描述,默认返回0就可以;
2.writeToParcel 方法。该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,以便从parcel容器获取数据,该方法声明如下:
writeToParcel (Parcel dest, int flags) 具体参数含义见javadoc
3.静态的Parcelable.Creator接口,本接口有两个方法:
createFromParcel(Parcel in) 从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
newArray(int size) 创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
代码实现
1.封装数据,把实现parcelable接口的Person对象用Intent传递到TwoActivity里;

        // 封装数据

        GetTrackingforMultiInfo Info= new GetTrackingforMultiInfo();
        Info.setlatitude("22.3333");
        Info.setlongitude("25.6666");
        // 用Intent传递Person对象
        Intent i = new Intent(this, TwoActivity.class);
        i.putExtra("GetTrackingforMultiInfo", Info);
        startActivity(i);
2.TwoActivity获取数据,从DemoActivity传递的Person对象给解析,并打印;
 GetTrackingforMultiInfo Info= (GetTrackingforMultiInfo)getIntent().getParcelableExtra("GetTrackingforMultiInfo");
        
        System.out.println("olatitude"+Info.getolatitude());
        System.out.println("olongitude"+Info.getolongitude);
3.实现Parcelable接口的实体类

/**
 {"status":"0","data":[{"deviceUtcDate":"2016-01-13 13:37:47",
 "olatitude":"22.99946","olongitude":"113.09002",
 "speed":0.00,"status":"2","course":0,"isStop":1,"icon":"27_0",
 "distance":600.9482,"acc":0,"GPS":0,"GSM":0,"Battery":0}]}
 */
public class GetTrackingforMultiInfo implements Parcelable{

//成员变量

    // 1.必须实现Parcelable.Creator接口,否则在获取Person数据的时候,会报错,如下:
    // android.os.BadParcelableException:
    // Parcelable protocol requires a Parcelable.Creator object called  CREATOR on class com.um.demo.Person
    // 2.这个接口实现了从Percel容器读取Person数据,并返回Person对象给逻辑层使用
    // 3.实现Parcelable.Creator接口对象名必须为CREATOR,不如同样会报错上面所提到的错;
    // 4.在读取Parcel容器里的数据事,必须按成员变量声明的顺序读取数据,不然会出现获取数据出错
    // 5.反序列化对象
    private String deviceUtcDate;
   // private String latitude;
   // private String longitude;
    private String olatitude;
    private String olongitude;
    private Double speed;
    private int status;
    private int course;
    private int isStop;
    private String icon;
    private Double distance;
    private int acc;
    private int GPS;
    private int GSM;
    private int Battery;

    private String deviceName;
    private String UserId;
    public GetTrackingforMultiInfo() {
        super();
    }
    public static final Parcelable.Creator CREATOR = new Creator(){
        @Override
        public Object createFromParcel(Parcel source) {
            GetTrackingforMultiInfo Info=new GetTrackingforMultiInfo();
    // 必须按成员变量声明的顺序读取数据,不然会出现获取数据出错
            Info.setDeviceUtcDate(source.readString());
            Info.setOlatitude(source.readString());
            Info.setOlongitude(source.readString());
            Info.setSpeed(source.readDouble());
            Info.setStatus(source.readInt());
            Info.setCourse(source.readInt());
            Info.setIsStop(source.readInt());
            Info.setIcon(source.readString());
            Info.setDistance(source.readDouble());
            Info.setAcc(source.readInt());
            Info.setGPS(source.readInt());
            Info.setGSM(source.readInt());
            Info.setBattery(source.readInt());
            Info.setDeviceName(source.readString());
            Info.setUserId(source.readString());
            return Info;
        }
        @Override
        public Object[] newArray(int size) {
            return new GetTrackingforMultiInfo[size];
        }
    };
    public GetTrackingforMultiInfo(
            String deviceUtcDate, String olatitude, String olongitude,
            Double speed, int status, int course, int isStop, String icon,
            Double distance, int acc, int GPS, int GSM, int battery,
            String deviceName, String userId) {
        this.deviceUtcDate = deviceUtcDate;
        this.olatitude = olatitude;
        this.olongitude = olongitude;
        this.speed = speed;
        this.status = status;
        this.course = course;
        this.isStop = isStop;
        this.icon = icon;
        this.distance = distance;
        this.acc = acc;
        this.GPS = GPS;
        this.GSM = GSM;
        Battery = battery;
        this.deviceName = deviceName;
        UserId = userId;
    }

    public String getDeviceUtcDate() {
        return deviceUtcDate;
    }

    public void setDeviceUtcDate(String deviceUtcDate) {
        this.deviceUtcDate = deviceUtcDate;
    }

    public String getOlatitude() {
        return olatitude;
    }

    public void setOlatitude(String olatitude) {
        this.olatitude = olatitude;
    }

    public String getOlongitude() {
        return olongitude;
    }

    public void setOlongitude(String olongitude) {
        this.olongitude = olongitude;
    }

    public Double getSpeed() {
        return speed;
    }

    public void setSpeed(Double speed) {
        this.speed = speed;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getCourse() {
        return course;
    }

    public void setCourse(int course) {
        this.course = course;
    }

    public int getIsStop() {
        return isStop;
    }

    public void setIsStop(int isStop) {
        this.isStop = isStop;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Double getDistance() {
        return distance;
    }

    public void setDistance(Double distance) {
        this.distance = distance;
    }

    public int getAcc() {
        return acc;
    }

    public void setAcc(int acc) {
        this.acc = acc;
    }

    public int getGPS() {
        return GPS;
    }

    public void setGPS(int GPS) {
        this.GPS = GPS;
    }

    public int getGSM() {
        return GSM;
    }

    public void setGSM(int GSM) {
        this.GSM = GSM;
    }

    public int getBattery() {
        return Battery;
    }

    public void setBattery(int battery) {
        Battery = battery;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public String getUserId() {
        return UserId;
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    @Override
    public String toString() {
        return "GetTrackingforMultiInfo{" +
                "deviceUtcDate='" + deviceUtcDate + '\'' +
                ", olatitude='" + olatitude + '\'' +
                ", olongitude='" + olongitude + '\'' +
                ", speed=" + speed +
                ", status=" + status +
                ", course=" + course +
                ", isStop=" + isStop +
                ", icon='" + icon + '\'' +
                ", distance=" + distance +
                ", acc=" + acc +
                ", GPS=" + GPS +
                ", GSM=" + GSM +
                ", Battery=" + Battery +
                ", deviceName='" + deviceName + '\'' +
                ", UserId='" + UserId + '\'' +
                '}';
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // 1.必须按成员变量声明的顺序封装数据,不然会出现获取数据出错
        // 2.序列化对象
        dest.writeString(deviceUtcDate);
        dest.writeString(olatitude);
        dest.writeString(olongitude);
        dest.writeDouble(speed);
        dest.writeInt(status);
        dest.writeInt(course);
        dest.writeInt(isStop);
        dest.writeString(icon);
        dest.writeDouble(distance);
        dest.writeInt(acc);
        dest.writeInt(GPS);
        dest.writeInt(GSM);
        dest.writeInt(Battery);
        dest.writeString(deviceName);
        dest.writeString(UserId);
    }

}




阅读更多
个人分类: Android Parcelable接口
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭