鴻蒙中的Ability之間或者進(jìn)程間數(shù)據(jù)傳遞之對(duì)象(Sequenceable序列化)
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
這兩天51cto上的一個(gè)粉絲朋友問了我一個(gè)問題,Ability之間使用Sequenceable序列化傳遞數(shù)據(jù),如何傳遞Uri類型數(shù)據(jù)?網(wǎng)上確實(shí)也沒有介紹這個(gè)使用的demo,為了幫他解決問題,自己幫他寫了一個(gè)demo,順手發(fā)布一篇博客和源代碼。
seralizable是在java api中的類,用它也可以實(shí)現(xiàn)序列化,而在android中也有一個(gè)類使對(duì)象序列化,那就是parcelable,而在HarmonyOS中用Sequenceable來進(jìn)行序列化。
那么它們之間有什么區(qū)別呢?
seralizable:序列化到本地,是一個(gè)持久化的操作,效率慢一點(diǎn)
parcelable:只存在于內(nèi)存,程序結(jié)束,序列化后的對(duì)象就不存在了。效率快一點(diǎn)
Sequenceable:等同parcelable在Android中的作用。
下面我編寫兩個(gè)AbilitySlice之間互相跳轉(zhuǎn)來傳遞數(shù)據(jù)
MainAbilitySlice對(duì)應(yīng)的布局文件代碼如下:
- <?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>
就是系統(tǒng)自動(dòng)生成的helloworld,我偷懶就沒修改了,核心不在這里。
再創(chuàng)建一個(gè)TestSlice,布局代碼如下:
- <?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>
為了要在兩個(gè)Slice中間傳遞一個(gè)序列化對(duì)象數(shù)據(jù),需要先創(chuàng)建一個(gè)實(shí)體類,并且實(shí)現(xiàn)Sequenceable接口,這里才是整個(gè)的核心代碼,如下:
- package com.xdw.sequencedemo;
- import ohos.utils.Parcel;
- import ohos.utils.Sequenceable;
- import ohos.utils.net.Uri;
- /**
- * Created by 夏德旺 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;
- }
- //上面是傳統(tǒng)的實(shí)體類的構(gòu)造函數(shù)和getter、setter
- //下面是序列化的核心
- //向包裹中寫入數(shù)據(jù),包裹可以理解為一塊內(nèi)存區(qū)
- public boolean marshalling(Parcel out) {
- out.writeSequenceable(uri); //注意Uri類型的寫法和普通數(shù)據(jù)類型有所不同
- return out.writeInt(number) && out.writeString(name);
- }
- //從包裹中讀取數(shù)據(jù)
- public boolean unmarshalling(Parcel in) {
- this.number = in.readInt();
- this.name = in.readString();
- return in.readSequenceable(uri); //注意Uri類型的寫法和普通數(shù)據(jù)類型有所不同
- }
- //序列化對(duì)象的內(nèi)部構(gòu)造器,必須實(shí)現(xiàn)
- public static final Sequenceable.Producer
- PRODUCER = new Sequenceable.Producer
- () {
- public Student createFromParcel(Parcel in) { //從包裹中獲取數(shù)據(jù)構(gòu)造對(duì)象
- // Initialize an instance first, then do customized unmarshlling.
- Student instance = new Student();
- instance.unmarshalling(in);
- return instance;
- } //必須實(shí)現(xiàn)Producer
- };
- }
下面編寫MainAbilitySlice的代碼,給Text控件添加一個(gè)點(diǎn)擊事件來跳轉(zhuǎn)頁(yè)面并且傳遞一個(gè)student參數(shù)
- 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("夏德旺");
- 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);
- }
- }
編寫TestSlice的代碼接收傳遞過來的student參數(shù),并且通過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 夏德旺 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);
- }
- }
到此,代碼編寫完成,下面是運(yùn)行測(cè)試圖:
這里也順便完美解決了之前51cto上的粉絲朋友問我的Sequenceable對(duì)象無法讀取Uri數(shù)據(jù)的問題。
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)