[Harmony OS] [JAVA UI] How to use Parcel to store data or transfer data in Hongmeng system

  1. About Parcel

    During the IPC process, the sender can use the write method provided by Parcel to write the data to be sent into the Parcel object in a specific format. The receiver can use the read method provided by Parcel to read data in a specific format from the Parcel object.

    The default capacity of a Parcel instance is 200KB. If you want more or less, change it with setCapacity(int).

    Note: Only data of the following data types can be written to or read from the package: byte, byteArray, short, shortArray, int, intArray, long, longArray, float, floatArray, double, doubleArray, boolean, booleanArray, char, charArray, String, StringArray, PlainBooleanArray, Serializable, Sequenceable, and SequenceableArray.

     

     

  2. Method introduction

    2.1 addAppClassLoader(ClassLoader newClassLoader) public void addAppClassLoader(ClassLoader newClassLoader) Add a third-party ClassLoader for instance initialization. If you need to encapsulate or unencapsulate custom serialized data, add your own ClassLoader.

    2.2 writeSequenceable public final void writeSequenceable(Sequenceable val) Writes the serializable object to the Parcel instance. For the rest of the methods, refer to the document Parcel data

     

  3. To encapsulate the Sequenceable entity class, we refer to the Sequenceable data

    image.png

     

  4. Hands

    Today, I will save a Sequenceable collection to Parcel and then read it out.
    4.1 Create two new classes. Class A users store specific attributes, and Class B is used to store the entity class collection of
    A. The code of Class A is as follows

     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 Class B is used to store the entity class collection of A, the code is as follows

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

     

    It should be noted that we need to call the addAppClassLoader method, which is prone to errors, as shown below

    image.png

    4.3 Create a new abilitySlice, and then write a control in xml to store and read the data in Parcel. The xml code is as follows

    <?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 Implement the click button event, and then store and read the data, the code is as follows

     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. Running effect
    All codes are as follows
    5.1xml

    <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 code

    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 The effect is as follows

     

    image.png

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4478396/blog/5510894