1. Parcel 簡介 在 IPC 過程中,發送方可以使用 Parcel 提供的寫方法,將待發送的數據以特定格式寫入Parcel對象。接收方可以使用 Parcel 提供的讀取方法從 Parcel 對象中讀取特定格式的數據。 Parcel 實例的預設容量為200KB。如果您想要更多或更少,請使用 s ...
1. Parcel 簡介
在 IPC 過程中,發送方可以使用 Parcel 提供的寫方法,將待發送的數據以特定格式寫入Parcel對象。接收方可以使用 Parcel 提供的讀取方法從 Parcel 對象中讀取特定格式的數據。
Parcel 實例的預設容量為200KB。如果您想要更多或更少,請使用 setCapacity(int) 更改它。
註意:只有以下數據類型的數據才能寫入或從包裹中讀取: byte, byteArray, short, shortArray, int, intArray, long, longArray, float, floatArray, double, doubleArray, boolean, booleanArray, char, charArray, String, StringArray、 PlainBooleanArray, Serializable, Sequenceable, 和 SequenceableArray。
2. 方法介紹
2.1 addAppClassLoader(ClassLoader newClassLoader)public void addAppClassLoader(ClassLoader newClassLoader)添加第三方ClassLoader用於實例初始化。如果您需要封裝或取消封裝自定義序列化數據,請添加您自己的ClassLoader。
2.2 writeSequenceablepublic final void writeSequenceable(Sequenceable val)將可序列化對象寫入Parcel實例。其餘的方法參考文檔Parcel資料
3. 封裝Sequenceable實體類我們參考Sequenceable資料
4. 動手實踐
今天將一個 Sequenceable 的集合存到 Parcel 然後讀取出來
4.1 新建兩個類 A類用戶存放具體屬性,B類用於存放A的實體類集合
A類代碼如下
public static class A implements Sequenceable {
private int a;
private int b;
private int c;
public A() {
this(0, 0, 0);
}
public A(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public boolean hasFileDescriptor() {
return false;
}
@Override
public boolean marshalling(Parcel out) {
out.writeInt(a);
out.writeInt(b);
out.writeInt(c);
return true;
}
@Override
public boolean unmarshalling(Parcel in) {
a = in.readInt();
b = in.readInt();
c = in.readInt();
return true;
}
public static final Sequenceable.Producer<A> PRODUCER = new Sequenceable.Producer<A>() {
public A createFromParcel(Parcel in) {
A instance = new A();
instance.unmarshalling(in);
return instance;
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A a1 = (A) o;
return a == a1.a &&
b == a1.b &&
c == a1.c;
}
@Override
public int hashCode() {
return Objects.hash(a, b, c);
}
@Override
public String toString() {
return "A{" +
"a=" + a +
", b=" + b +
", c=" + c +
'}';
}
}
4.2 B類用於存放A的實體類集合,代碼如下
public static class B implements Sequenceable {
private List<A> list;
private B() {
}
private B(List<A> list) {
this.list = list;
}
@Override
public boolean hasFileDescriptor() {
return false;
}
@Override
public boolean marshalling(Parcel out) {
out.writeSequenceableList(list);
return true;
}
@Override
public boolean unmarshalling(Parcel in) {
in.addAppClassLoader(getClass().getClassLoader());
list = in.readSequenceableList(A.class);
return true;
}
public static final Sequenceable.Producer<B> PRODUCER = new Sequenceable.Producer<B>() {
public B createFromParcel(Parcel in) {
B instance = new B();
instance.unmarshalling(in);
return instance;
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
B b = (B) o;
return Objects.equals(list, b.list);
}
@Override
public int hashCode() {
return Objects.hash(list);
}
}
需要註意的是我們需要調用addAppClassLoader方法,這個地方比較容易出錯,如下圖
4.3 新建abilitySlice ,然後在xml寫一個控制項用於存讀Parcel中數據,xml代碼如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:id="$+id:bt1"
ohos:height="100vp"
ohos:background_element="#Ed6262"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:text_size="30fp"
ohos:text_color="#ffffff"
ohos:text="存儲數據"/>
<Text
ohos:top_margin="10vp"
ohos:id="$+id:Result"
ohos:height="100vp"
ohos:multiple_lines="true"
ohos:width="match_parent"
ohos:text_alignment="center|left"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
/>
</DirectionalLayout>
4.4 實現點擊按鈕事件,然後存讀數據,代碼如下
findComponentById(ResourceTable.Id_bt1).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
List<A> list = new ArrayList<>();
A a = new A(1, 2, 3);
A a2 = new A(2, 2, 3);
A a3 = new A(3, 2, 3);
list.add(a);
list.add(a2);
list.add(a3);
B b = new B(list);
Parcel parcel = Parcel.create();
parcel.writeSequenceable(b);
B b1 = new B();
parcel.readSequenceable(b1);
StringBuilder stringBuilder=new StringBuilder();
if (b1.list.size() > 0) {
for (int i = 0; i < b1.list.size(); i++) {
stringBuilder.append(" b1.list====>" + b1.list.get(i).toString());
stringBuilder.append("\n");
}
}
mResult.setText(stringBuilder.toString());
}
});
5. 運行效果
全部代碼如下
5.1 xml
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:id="$+id:bt1"
ohos:height="100vp"
ohos:background_element="#Ed6262"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:text_size="30fp"
ohos:text_color="#ffffff"
ohos:text="存儲數據"/>
<Text
ohos:top_margin="10vp"
ohos:id="$+id:Result"
ohos:height="100vp"
ohos:multiple_lines="true"
ohos:width="match_parent"
ohos:text_alignment="center|left"
ohos:text_size="20fp"
ohos:text_color="#ed6262"
/>
</DirectionalLayout>
5.2 java 代碼
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.utils.Parcel;
import ohos.utils.Sequenceable;
import java.util.*;
public class ParcelTestSlice extends AbilitySlice {
private Text mResult;
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_slice_main2);
mResult= (Text) findComponentById(ResourceTable.Id_Result);
findComponentById(ResourceTable.Id_bt1).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
List<A> list = new ArrayList<>();
A a = new A(1, 2, 3);
A a2 = new A(2, 2, 3);
A a3 = new A(3, 2, 3);
list.add(a);
list.add(a2);
list.add(a3);
B b = new B(list);
Parcel parcel = Parcel.create();
parcel.writeSequenceable(b);
B b1 = new B();
parcel.readSequenceable(b1);
StringBuilder stringBuilder=new StringBuilder();
if (b1.list.size() > 0) {
for (int i = 0; i < b1.list.size(); i++) {
stringBuilder.append(" b1.list====>" + b1.list.get(i).toString());
stringBuilder.append("\n");
}
}
mResult.setText(stringBuilder.toString());
}
});
}
public static class B implements Sequenceable {
private List<A> list;
private B() {}
private B(List<A> list) {
this.list = list;
}
@Override
public boolean hasFileDescriptor() {
return false;
}
@Override
public boolean marshalling(Parcel out) {
out.writeSequenceableList(list);
return true;
}
@Override
public boolean unmarshalling(Parcel in) {
in.addAppClassLoader(getClass().getClassLoader());
list = in.readSequenceableList(A.class);
return true;
}
public static final Sequenceable.Producer<B> PRODUCER = new Sequenceable.Producer<B>() {
public B createFromParcel(Parcel in) {
B instance = new B();
instance.unmarshalling(in);
return instance;
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
B b = (B) o;
return Objects.equals(list, b.list);
}
@Override
public int hashCode() {
return Objects.hash(list);
}
}
public static class A implements Sequenceable {
private int a;
private int b;
private int c;
public A() {
this(0, 0, 0);
}
public A(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public boolean hasFileDescriptor() {
return false;
}
@Override
public boolean marshalling(Parcel out) {
out.writeInt(a);
out.writeInt(b);
out.writeInt(c);
return true;
}
@Override
public boolean unmarshalling(Parcel in) {
a = in.readInt();
b = in.readInt();
c = in.readInt();
return true;
}
public static final Sequenceable.Producer<A> PRODUCER = new Sequenceable.Producer<A>() {
public A createFromParcel(Parcel in) {
A instance = new A();
instance.unmarshalling(in);
return instance;
}
};
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
A a1 = (A) o;
return a == a1.a &&
b == a1.b &&
c == a1.c;
}
@Override
public int hashCode() {
return Objects.hash(a, b, c);
}
@Override
public String toString() {
return "A{" +
"a=" + a +
", b=" + b +
", c=" + c +
'}';
}
}
}
5.3 效果如下