日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

鸿蒙ability怎么理解,鸿蒙中的Ability之间或者进程间数据传递之对象(Sequenceable序列化)...

發布時間:2023/12/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 鸿蒙ability怎么理解,鸿蒙中的Ability之间或者进程间数据传递之对象(Sequenceable序列化)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這兩天51cto上的一個粉絲朋友問了我一個問題,Ability之間使用Sequenceable序列化傳遞數據,如何傳遞Uri類型數據?網上確實也沒有介紹這個使用的demo,為了幫他解決問題,自己幫他寫了一個demo,順手發布一篇博客和源代碼。

seralizable是在java api中的類,用它也可以實現序列化,而在android中也有一個類使對象序列化,那就是parcelable,而在HarmonyOS中用Sequenceable來進行序列化。

那么它們之間有什么區別呢?

seralizable:序列化到本地,是一個持久化的操作,效率慢一點

parcelable:只存在于內存,程序結束,序列化后的對象就不存在了。效率快一點

Sequenceable:等同parcelable在Android中的作用。

下面我編寫兩個AbilitySlice之間互相跳轉來傳遞數據

MainAbilitySlice對應的布局文件代碼如下:

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical">

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"

/>

就是系統自動生成的helloworld,我偷懶就沒修改了,核心不在這里。

再創建一個TestSlice,布局代碼如下:

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:orientation="vertical">

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"

/>

為了要在兩個Slice中間傳遞一個序列化對象數據,需要先創建一個實體類,并且實現Sequenceable接口,這里才是整個的核心代碼,如下:

package?com.xdw.sequencedemo;

import?ohos.utils.Parcel;

import?ohos.utils.Sequenceable;

import?ohos.utils.net.Uri;

/**

*?Created?by夏德旺on2021/2/26?10:39

*/

publicclass?Student?implements?Sequenceable?{

private?intnumber;

private?String?name;

private?Uri?uri;

publicStudent()?{

}

publicStudent(intnumber,?Stringname,?Uri?uri)?{

this.number?=?number;

this.name=name;

this.uri?=?uri;

}

publicintgetNumber()?{

returnnumber;

}

publicvoid?setNumber(intnumber)?{

this.number?=?number;

}

publicString?getName()?{

returnname;

}

publicvoid?setName(Stringname)?{

this.name=name;

}

publicUri?getUri()?{

returnuri;

}

publicvoid?setUri(Uri?uri)?{

this.uri?=?uri;

}

//上面是傳統的實體類的構造函數和getter、setter

//下面是序列化的核心

//向包裹中寫入數據,包裹可以理解為一塊內存區

publicboolean?marshalling(Parcelout)?{

out.writeSequenceable(uri);?//注意Uri類型的寫法和普通數據類型有所不同

returnout.writeInt(number)?&&out.writeString(name);

}

//從包裹中讀取數據

publicboolean?unmarshalling(Parcelin)?{

this.number?=?in.readInt();

this.name=in.readString();

returnin.readSequenceable(uri);????//注意Uri類型的寫法和普通數據類型有所不同

}

//序列化對象的內部構造器,必須實現

publicstaticfinal?Sequenceable.Producer

PRODUCER?=?new?Sequenceable.Producer

()?{

publicStudent?createFromParcel(Parcelin)?{????//從包裹中獲取數據構造對象

//?Initialize?an?instance?first,thendo?customized?unmarshlling.

Student?instance?=?new?Student();

instance.unmarshalling(in);

returninstance;

}???//必須實現Producer

};

}

下面編寫MainAbilitySlice的代碼,給Text控件添加一個點擊事件來跳轉頁面并且傳遞一個student參數

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;

publicclass?MainAbilitySlice?extends?AbilitySlice?{

private?Text?text;

@Override

publicvoid?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

publicvoid?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

publicvoid?onActive()?{

super.onActive();

}

@Override

publicvoid?onForeground(Intent?intent)?{

super.onForeground(intent);

}

}

編寫TestSlice的代碼接收傳遞過來的student參數,并且通過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夏德旺on2021/2/26?10:39

*/

publicclass?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);

}

}

到此,代碼編寫完成,下面是運行測試圖:

這里也順便完美解決了之前51cto上的粉絲朋友問我的Sequenceable對象無法讀取Uri數據的問題。

【編輯推薦】

【責任編輯:jianghua TEL:(010)68476606】

點贊 0

總結

以上是生活随笔為你收集整理的鸿蒙ability怎么理解,鸿蒙中的Ability之间或者进程间数据传递之对象(Sequenceable序列化)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。