自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙中的Ability之間或者進(jìn)程間數(shù)據(jù)傳遞之對(duì)象(Sequenceable序列化)

開發(fā)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com

[[385512]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

這兩天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)的布局文件代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <Text 
  9.         ohos:id="$+id:text_helloworld" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content" 
  12.         ohos:background_element="$graphic:background_ability_main" 
  13.         ohos:layout_alignment="horizontal_center" 
  14.         ohos:text="Hello World" 
  15.         ohos:text_size="50" 
  16.     /> 
  17.  
  18. </DirectionalLayout> 

就是系統(tǒng)自動(dòng)生成的helloworld,我偷懶就沒修改了,核心不在這里。

再創(chuàng)建一個(gè)TestSlice,布局代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <Text 
  9.         ohos:id="$+id:text_helloworld" 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_content" 
  12.         ohos:background_element="$graphic:background_ability_main" 
  13.         ohos:layout_alignment="horizontal_center" 
  14.         ohos:text="TEST" 
  15.         ohos:text_size="50" 
  16.     /> 
  17.  
  18. </DirectionalLayout> 

為了要在兩個(gè)Slice中間傳遞一個(gè)序列化對(duì)象數(shù)據(jù),需要先創(chuàng)建一個(gè)實(shí)體類,并且實(shí)現(xiàn)Sequenceable接口,這里才是整個(gè)的核心代碼,如下:

  1. package com.xdw.sequencedemo; 
  2.  
  3. import ohos.utils.Parcel; 
  4. import ohos.utils.Sequenceable; 
  5. import ohos.utils.net.Uri; 
  6.  
  7. /** 
  8.  * Created by 夏德旺 on 2021/2/26 10:39 
  9.  */ 
  10. public class Student implements Sequenceable { 
  11.     private int number; 
  12.  
  13.     private String name
  14.  
  15.     private Uri uri; 
  16.  
  17.  
  18.     public Student() { 
  19.     } 
  20.  
  21.     public Student(int number, String name, Uri uri) { 
  22.         this.number = number; 
  23.         this.name = name
  24.         this.uri = uri; 
  25.     } 
  26.  
  27.     public int getNumber() { 
  28.         return number; 
  29.     } 
  30.  
  31.     public void setNumber(int number) { 
  32.         this.number = number; 
  33.     } 
  34.  
  35.     public String getName() { 
  36.         return name
  37.     } 
  38.  
  39.     public void setName(String name) { 
  40.         this.name = name
  41.     } 
  42.  
  43.     public Uri getUri() { 
  44.         return uri; 
  45.     } 
  46.  
  47.     public void setUri(Uri uri) { 
  48.         this.uri = uri; 
  49.     } 
  50.  
  51.     //上面是傳統(tǒng)的實(shí)體類的構(gòu)造函數(shù)和getter、setter 
  52.     //下面是序列化的核心 
  53.     //向包裹中寫入數(shù)據(jù),包裹可以理解為一塊內(nèi)存區(qū) 
  54.     public boolean marshalling(Parcel out) { 
  55.         out.writeSequenceable(uri); //注意Uri類型的寫法和普通數(shù)據(jù)類型有所不同 
  56.         return out.writeInt(number) && out.writeString(name); 
  57.     } 
  58.  
  59.     //從包裹中讀取數(shù)據(jù) 
  60.     public boolean unmarshalling(Parcel in) { 
  61.         this.number = in.readInt(); 
  62.         this.name = in.readString(); 
  63.         return in.readSequenceable(uri);    //注意Uri類型的寫法和普通數(shù)據(jù)類型有所不同 
  64.     } 
  65.  
  66.     //序列化對(duì)象的內(nèi)部構(gòu)造器,必須實(shí)現(xiàn) 
  67.     public static final Sequenceable.Producer 
  68.             PRODUCER = new Sequenceable.Producer 
  69.             () { 
  70.         public Student createFromParcel(Parcel in) {    //從包裹中獲取數(shù)據(jù)構(gòu)造對(duì)象 
  71.             // Initialize an instance firstthen do customized unmarshlling. 
  72.             Student instance = new Student(); 
  73.             instance.unmarshalling(in); 
  74.             return instance; 
  75.         }   //必須實(shí)現(xiàn)Producer 
  76.     }; 

下面編寫MainAbilitySlice的代碼,給Text控件添加一個(gè)點(diǎn)擊事件來跳轉(zhuǎn)頁(yè)面并且傳遞一個(gè)student參數(shù)

  1. package com.xdw.sequencedemo.slice; 
  2.  
  3. import com.xdw.sequencedemo.ResourceTable; 
  4. import com.xdw.sequencedemo.Student; 
  5. import ohos.aafwk.ability.AbilitySlice; 
  6. import ohos.aafwk.content.Intent; 
  7. import ohos.agp.components.Component; 
  8. import ohos.agp.components.Text; 
  9. import ohos.agp.window.dialog.ToastDialog; 
  10. import ohos.utils.net.Uri; 
  11.  
  12. public class MainAbilitySlice extends AbilitySlice { 
  13.     private Text text; 
  14.     @Override 
  15.     public void onStart(Intent intent) { 
  16.         super.onStart(intent); 
  17.         super.setUIContent(ResourceTable.Layout_ability_main); 
  18.         text = (Text)findComponentById(ResourceTable.Id_text_helloworld); 
  19.         text.setClickedListener(new Component.ClickedListener() { 
  20.             @Override 
  21.             public void onClick(Component component) { 
  22.                 Intent intent1 = new Intent(); 
  23.                 Student student = new Student(); 
  24.                 student.setNumber(1); 
  25.                 student.setName("夏德旺"); 
  26.                 Uri uri = Uri.parse("http://www.xiadewang.com:8080/login?username=xdw&password=123"); 
  27.                 String scheme = uri.getScheme(); 
  28.                 //new ToastDialog(getContext()).setText("scheme="+scheme).show(); 
  29.                 student.setUri(uri); 
  30.                 intent1.setParam("student",student); 
  31.                 present(new TestSlice(),intent1); 
  32.             } 
  33.         }); 
  34.     } 
  35.  
  36.     @Override 
  37.     public void onActive() { 
  38.         super.onActive(); 
  39.     } 
  40.  
  41.     @Override 
  42.     public void onForeground(Intent intent) { 
  43.         super.onForeground(intent); 
  44.     } 

編寫TestSlice的代碼接收傳遞過來的student參數(shù),并且通過toast展示

  1. package com.xdw.sequencedemo.slice; 
  2.  
  3. import com.xdw.sequencedemo.ResourceTable; 
  4. import com.xdw.sequencedemo.Student; 
  5. import ohos.aafwk.ability.AbilitySlice; 
  6. import ohos.aafwk.content.Intent; 
  7. import ohos.agp.window.dialog.ToastDialog; 
  8. import ohos.utils.net.Uri; 
  9.  
  10. /** 
  11.  * Created by 夏德旺 on 2021/2/26 10:39 
  12.  */ 
  13. public class TestSlice  extends AbilitySlice { 
  14.     @Override 
  15.     protected void onStart(Intent intent) { 
  16.         super.onStart(intent); 
  17.         super.setUIContent(ResourceTable.Layout_slice_test); 
  18.         if(intent!=null){ 
  19.             Student student = intent.getSequenceableParam("student"); 
  20.             String name = student.getName(); 
  21.             Uri uri = student.getUri(); 
  22.             //new ToastDialog(getContext()).setText("name="+name).show(); 
  23.             new ToastDialog(getContext()).setText("scheme="+uri.getScheme()).show(); 
  24.         } 
  25.     } 
  26.  
  27.     @Override 
  28.     protected void onActive() { 
  29.         super.onActive(); 
  30.     } 
  31.  
  32.     @Override 
  33.     protected void onForeground(Intent intent) { 
  34.         super.onForeground(intent); 
  35.     } 

到此,代碼編寫完成,下面是運(yùn)行測(cè)試圖:

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

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2011-06-01 15:05:02

序列化反序列化

2018-03-19 10:20:23

Java序列化反序列化

2009-06-14 22:01:27

Java對(duì)象序列化反序列化

2012-04-13 10:45:59

XML

2011-04-02 13:47:01

2009-09-09 14:45:41

XML序列化和反序列化

2009-09-09 15:47:27

XML序列化和反序列化

2023-12-13 13:49:52

Python序列化模塊

2023-11-13 23:06:52

Android序列化

2011-07-13 09:31:48

ASP.NET數(shù)據(jù)傳遞

2021-08-30 12:25:12

Python序列化函數(shù)

2011-06-01 14:26:11

序列化

2009-03-10 13:38:01

Java序列化字節(jié)流

2022-08-06 08:41:18

序列化反序列化Hessian

2009-12-16 09:16:53

ASP.NET頁(yè)面間數(shù)

2016-12-05 18:32:08

序列化androidjava

2010-05-14 10:55:04

java對(duì)象序列化

2021-09-09 18:42:12

React 組件數(shù)據(jù)

2012-02-14 10:29:02

Java

2009-08-25 15:15:08

C#對(duì)象序列化應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)