Objects for data transfer between capabilities or between processes (Sequenceable serialization)

Hongmeng introductory guide, Xiaobai, come quickly! 0 basic learning route sharing, efficient learning methods, key question answering and puzzle solving -- > [course entrance]

In the past two days, a fan friend on 51cto asked me a question: how to transfer Uri type data between abilities using Sequenceable serialization? There is really no demo on the Internet. In order to help him solve the problem, I wrote a demo for him and published a blog and source code.

Serializable is a class in java api, which can also be used to realize serialization. In android, there is also a class to serialize objects, that is, parcelable, while in harmony OS, Sequenceable is used for serialization.

So what's the difference between them?

Serializable: serialization to the local is a persistent operation, which is a little slower

parcelable: only exists in memory. When the program ends, the serialized object does not exist. Be more efficient
Sequenceable: equivalent to the role of parcelable in Android.

 

Next, I write two AbilitySlice to jump between each other to transfer data

The layout file code corresponding to MainAbilitySlice 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:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="Hello World"
        ohos:text_size="50"
    />

</DirectionalLayout>

It's the helloworld automatically generated by the system. I'm lazy and haven't modified it. The core is not here.

Create another TestSlice. The layout 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:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:background_element="$graphic:background_ability_main"
        ohos:layout_alignment="horizontal_center"
        ohos:text="TEST"
        ohos:text_size="50"
    />

</DirectionalLayout>

In order to pass a serialized object data between two slices, you need to create an entity class and implement the Sequenceable interface. Here is the whole core code, as follows:

package com.xdw.sequencedemo;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;
import ohos.utils.net.Uri;

/**
 * Created by Xia Dewang on 2021/2/26 10:39
 */
public class Student implements Sequenceable {
    private int number;

    private String name;

    private Uri uri;


    public Student() {
    }

    public Student(int number, String name, Uri uri) {
        this.number = number;
        this.name = name;
        this.uri = uri;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }

    //The above is the constructor, getter and setter of the traditional entity class
    //Here is the core of serialization
    //Write data to the package, which can be understood as a memory area
    public boolean marshalling(Parcel out) {
        out.writeSequenceable(uri); //Note that the Uri type is written differently from the normal data type
        return out.writeInt(number) && out.writeString(name);
    }

    //Read data from package
    public boolean unmarshalling(Parcel in) {
        this.number = in.readInt();
        this.name = in.readString();
        return in.readSequenceable(uri);    //Note that the Uri type is written differently from the normal data type
    }

    //The internal constructor of the serialized object must be implemented
    public static final Sequenceable.Producer
            PRODUCER = new Sequenceable.Producer
            () {
        public Student createFromParcel(Parcel in) {    //Get data construction object from package
            // Initialize an instance first, then do customized unmarshlling.
            Student instance = new Student();
            instance.unmarshalling(in);
            return instance;
        }   //Producer must be implemented
    };
}

 

Next, write the code of MainAbilitySlice, add a click event to the Text control to jump to the page and pass a student parameter

package com.xdw.sequencedemo.slice;

import com.xdw.sequencedemo.ResourceTable;
import com.xdw.sequencedemo.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.net.Uri;

public class MainAbilitySlice extends AbilitySlice {
    private Text text;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        text = (Text)findComponentById(ResourceTable.Id_text_helloworld);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent1 = new Intent();
                Student student = new Student();
                student.setNumber(1);
                student.setName("Xia Dewang");
                Uri uri = Uri.parse("http://www.xiadewang.com:8080/login?username=xdw&password=123");
                String scheme = uri.getScheme();
                //new ToastDialog(getContext()).setText("scheme="+scheme).show();
                student.setUri(uri);
                intent1.setParam("student",student);
                present(new TestSlice(),intent1);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

 

Write the code of TestSlice to receive the passed student parameters and display them through toast

package com.xdw.sequencedemo.slice;

import com.xdw.sequencedemo.ResourceTable;
import com.xdw.sequencedemo.Student;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.net.Uri;

/**
 * Created by Xia Dewang on 2021/2/26 10:39
 */
public class TestSlice  extends AbilitySlice {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_test);
        if(intent!=null){
            Student student = intent.getSequenceableParam("student");
            String name = student.getName();
            Uri uri = student.getUri();
            //new ToastDialog(getContext()).setText("name="+name).show();
            new ToastDialog(getContext()).setText("scheme="+uri.getScheme()).show();
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

 

At this point, the code is written. The following is the running test diagram

By the way, it also perfectly solves the problem that my Sequenceable object can't read Uri data asked by fans on 51cto.

Download demo source code

Author: soft Tong Xia Dewang

For more information, please visit the Hongmeng community jointly built by 51CTO and Huawei: https://harmonyos.51cto.com

Tags: Android harmonyos

Posted by Blondy on Fri, 15 Apr 2022 07:08:10 +0930