成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

鴻蒙中的Ability之間或者進程間數據傳遞之對象(Sequenceable序列化)

開發
文章由鴻蒙社區產出,想要了解更多內容請前往:51CTO和華為官方戰略合作共建的鴻蒙技術社區https://harmonyos.51cto.com

[[385512]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

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

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

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

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

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

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

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

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

  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> 

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

再創建一個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> 

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

  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.     //上面是傳統的實體類的構造函數和getter、setter 
  52.     //下面是序列化的核心 
  53.     //向包裹中寫入數據,包裹可以理解為一塊內存區 
  54.     public boolean marshalling(Parcel out) { 
  55.         out.writeSequenceable(uri); //注意Uri類型的寫法和普通數據類型有所不同 
  56.         return out.writeInt(number) && out.writeString(name); 
  57.     } 
  58.  
  59.     //從包裹中讀取數據 
  60.     public boolean unmarshalling(Parcel in) { 
  61.         this.number = in.readInt(); 
  62.         this.name = in.readString(); 
  63.         return in.readSequenceable(uri);    //注意Uri類型的寫法和普通數據類型有所不同 
  64.     } 
  65.  
  66.     //序列化對象的內部構造器,必須實現 
  67.     public static final Sequenceable.Producer 
  68.             PRODUCER = new Sequenceable.Producer 
  69.             () { 
  70.         public Student createFromParcel(Parcel in) {    //從包裹中獲取數據構造對象 
  71.             // Initialize an instance firstthen do customized unmarshlling. 
  72.             Student instance = new Student(); 
  73.             instance.unmarshalling(in); 
  74.             return instance; 
  75.         }   //必須實現Producer 
  76.     }; 

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

  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參數,并且通過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.     } 

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

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

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2011-06-01 15:05:02

序列化反序列化

2018-03-19 10:20:23

Java序列化反序列化

2009-06-14 22:01:27

Java對象序列化反序列化

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數據傳遞

2021-08-30 12:25:12

Python序列化函數

2011-06-01 14:26:11

序列化

2009-03-10 13:38:01

Java序列化字節流

2022-08-06 08:41:18

序列化反序列化Hessian

2009-12-16 09:16:53

ASP.NET頁面間數

2016-12-05 18:32:08

序列化androidjava

2010-05-14 10:55:04

java對象序列化

2021-09-09 18:42:12

React 組件數據

2012-02-14 10:29:02

Java

2009-08-25 15:15:08

C#對象序列化應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产中文视频 | 国产9久 | 中文字幕亚洲一区二区三区 | 亚洲视频中文字幕 | 国产在线资源 | 欧美性一级 | 欧美日韩成人影院 | 久久性色 | 在线播放国产一区二区三区 | 久久久久网站 | 午夜精品视频在线观看 | www.蜜桃av| 欧美一区二区三区 | 91成人精品视频 | 视频一区 亚洲 | 日本午夜在线视频 | 男女网站在线观看 | 免费一级做a爰片久久毛片潮喷 | 国产精品福利网站 | 亚洲综合一区二区三区 | 91精品国产美女在线观看 | 91福利电影在线观看 | 欧美乱码精品一区二区三区 | 日韩喷潮 | 一级黄片一级毛片 | 久久久久99 | 黄网在线观看 | 九九热在线观看视频 | 蜜桃毛片 | 狠狠操你 | 欧美日韩国产高清 | 日日碰狠狠躁久久躁96avv | 一级毛片在线播放 | 国产精品久久二区 | 国产精品久久久久久吹潮 | 欧美日韩在线综合 | av在线免费观看网址 | 午夜精品一区二区三区在线观看 | 色婷婷综合网站 | 99爱在线免费观看 | 国产99在线 | 欧美 |