Android使用AIDL时传递复杂数据对象的写法


友情提示:阅读本篇文章之前需要对AIDL有基本的认识,如果还未了解AIDL是什么的小伙伴,请自行查阅相关材料。
我们在使用AIDL时,经常需要传递数据,有时需要传递的数据是复杂对象,本篇文章就来说说传递复杂对象时如何写,主要是readFromParcel()和writeToParcel()方法里读写字段的写法。

注意:readFromParcel()和writeToParcel()方法里对字段的操作顺序必须和字段在对象里声明时的顺序一样,否则会出错。

1、字段为基本数据类型时的写法

public class Person implements Parcelable {

    private String name;
    private int age;
    private long workingDay;
    private double money;
    private float expenditure;
    private byte[] avatar;

    public Person(Parcel source) {
        readFromParcel(source);
    }

    public void readFromParcel(Parcel source) {
        this.name = source.readString();
        this.age = source.readInt();
        this.workingDay = source.readLong();
        this.money = source.readDouble();
        this.expenditure = source.readFloat();
        source.readByteArray(this.avatar);
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeLong(workingDay);
        dest.writeDouble(money);
        dest.writeFloat(expenditure);
        dest.writeByteArray(avatar);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}


2、字段为对象时的写法

public class Son implements Parcelable {
    private String name;
    private int age;

    public Son() {
    }

    public Son(Parcel source) {
        readFromParcel(source);
    }

    public void readFromParcel(Parcel source) {
        this.name = source.readString();
        this.age = source.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Son> CREATOR = new Creator<Son>() {
        public Son createFromParcel(Parcel source) {
            return new Son(source);
        }

        public Son[] newArray(int size) {
            return new Son[size];
        }
    };
}

public class Person implements Parcelable {

    private Son son;

    public Person(Parcel source) {
        readFromParcel(source);
    }

    public void readFromParcel(Parcel source) {
        son = source.readParcelable(Son.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(son, flags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}

3、字段为Map和List类型时的写法
注意:
Map:只支持HashMap,里面的每个元素都必须被AIDL支持,包括key和value
List:只支持ArrayList,里面每个元素都必须能够被AIDL支持

public class Person implements Parcelable {

    private Map<String, String> mapString;
    private Map<String, Son> sonMap;

    private List<String> listString;
    private List<Son> sonList;


    public Person() {
    }

    public Person(Parcel source) {
        readFromParcel(source);
    }

    public void readFromParcel(Parcel source) {
        mapString = source.readHashMap(HashMap.class.getClassLoader());
        sonMap = source.readHashMap(Son.class.getClassLoader());

        listString = source.createStringArrayList();

        List<Son> sonList = new ArrayList<>();
        source.readTypedList(sonList, Son.CREATOR);
        this.sonList = sonList;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeMap(mapString);
        dest.writeMap(sonMap);
        dest.writeStringList(listString);
        dest.writeTypedList(sonList);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}

4、字段为枚举对象时的写法
public enum Color {

    RED("红色"),
    GREEN("绿色"),
    BLUE("蓝色"),
    BLACK("黑色"),
    WHITE("白色");

    private String colorText;
    Color(String colorText) {
        this.colorText = colorText;
    }

    public String getColorText() {
        return colorText;
    }
}

public class Person implements Parcelable {

    private Color color;

    public Person() {
    }

    public Person(Parcel source) {
        readFromParcel(source);
    }

    public void readFromParcel(Parcel source) {
        color = (Color) source.readSerializable();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeSerializable(color);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值