1. 程式人生 > >Android使用AIDL時傳遞複雜資料物件的寫法

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];
        }
    };
}