友情提示:阅读本篇文章之前需要对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];
}
};
}