码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【Harmony OS】【JAVA UI】鸿蒙系统中怎么使用Parcel进行存储数据或数据传递


    1.  Parcel简介

      在IPC过程中,发送方可以使用Parcel提供的写方法,将待发送的数据以特定格式写入Parcel对象。接收方可以使用Parcel提供的读取方法从Parcel对象中读取特定格式的数据。

      Parcel实例的默认容量为200KB。如果您想要更多或更少,请使用setCapacity(int)更改它。

      注意:只有以下数据类型的数据才能写入或从包裹中读取: byte, byteArray, short, shortArray, int, intArray, long, longArray, float, floatArray, double, doubleArray, boolean, booleanArray, char, charArray, String, StringArray、 PlainBooleanArray, Serializable, Sequenceable, 和 SequenceableArray。

    2. 方法介绍

      2.1 addAppClassLoader(ClassLoader newClassLoader)public void addAppClassLoader(ClassLoader newClassLoader)添加第三方ClassLoader用于实例初始化。如果您需要封装或取消封装自定义序列化数据,请添加您自己的ClassLoader。

      2.2 writeSequenceablepublic final void writeSequenceable(Sequenceable val)将可序列化对象写入Parcel实例。其余的方法参考文档Parcel资料

    3. 封装Sequenceable实体类我们参考Sequenceable资料

    4. 动手实践

      今天将一个Sequenceable的集合存到Parcel然后读取出来
      4.1 新建两个类 A类用户存放具体属性,B类用于存放A的实体类集合
      A类代码如下

      1. public static class A implements Sequenceable {
      2. private int a;
      3. private int b;
      4. private int c;
      5. public A() {
      6. this(0, 0, 0);
      7. }
      8. public A(int a, int b, int c) {
      9. this.a = a;
      10. this.b = b;
      11. this.c = c;
      12. }
      13. @Override
      14. public boolean hasFileDescriptor() {
      15. return false;
      16. }
      17. @Override
      18. public boolean marshalling(Parcel out) {
      19. out.writeInt(a);
      20. out.writeInt(b);
      21. out.writeInt(c);
      22. return true;
      23. }
      24. @Override
      25. public boolean unmarshalling(Parcel in) {
      26. a = in.readInt();
      27. b = in.readInt();
      28. c = in.readInt();
      29. return true;
      30. }
      31. public static final Sequenceable.Producer PRODUCER = new Sequenceable.Producer() {
      32. public A createFromParcel(Parcel in) {
      33. A instance = new A();
      34. instance.unmarshalling(in);
      35. return instance;
      36. }
      37. };
      38. @Override
      39. public boolean equals(Object o) {
      40. if (this == o) return true;
      41. if (o == null || getClass() != o.getClass()) return false;
      42. A a1 = (A) o;
      43. return a == a1.a &&
      44. b == a1.b &&
      45. c == a1.c;
      46. }
      47. @Override
      48. public int hashCode() {
      49. return Objects.hash(a, b, c);
      50. }
      51. @Override
      52. public String toString() {
      53. return "A{" +
      54. "a=" + a +
      55. ", b=" + b +
      56. ", c=" + c +
      57. '}';
      58. }

      4.2 B类用于存放A的实体类集合,代码如下

      1. public static class B implements Sequenceable {
      2. private List list;
      3. private B() {
      4. }
      5. private B(List list) {
      6. this.list = list;
      7. }
      8. @Override
      9. public boolean hasFileDescriptor() {
      10. return false;
      11. }
      12. @Override
      13. public boolean marshalling(Parcel out) {
      14. out.writeSequenceableList(list);
      15. return true;
      16. }
      17. @Override
      18. public boolean unmarshalling(Parcel in) {
      19. in.addAppClassLoader(getClass().getClassLoader());
      20. list = in.readSequenceableList(A.class);
      21. return true;
      22. }
      23. public static final Sequenceable.Producer PRODUCER = new Sequenceable.Producer() {
      24. public B createFromParcel(Parcel in) {
      25. B instance = new B();
      26. instance.unmarshalling(in);
      27. return instance;
      28. }
      29. };
      30. @Override
      31. public boolean equals(Object o) {
      32. if (this == o) return true;
      33. if (o == null || getClass() != o.getClass()) return false;
      34. B b = (B) o;
      35. return Objects.equals(list, b.list);
      36. }
      37. @Override
      38. public int hashCode() {
      39. return Objects.hash(list);
      40. }
      41. }

      需要注意的是我们需要调用addAppClassLoader方法,这个地方比较容易出错,如下图

      4.3新建abilitySlice ,然后在xml写一个控件用于存读Parcel中数据,xml代码如下

      1. <DirectionalLayout
      2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
      3. ohos:height="match_parent"
      4. ohos:width="match_parent"
      5. ohos:orientation="vertical">
      6. <Text
      7. ohos:id="$+id:bt1"
      8. ohos:height="100vp"
      9. ohos:background_element="#Ed6262"
      10. ohos:width="match_parent"
      11. ohos:text_alignment="center"
      12. ohos:text_size="30fp"
      13. ohos:text_color="#ffffff"
      14. ohos:text="存储数据"/>
      15. <Text
      16. ohos:top_margin="10vp"
      17. ohos:id="$+id:Result"
      18. ohos:height="100vp"
      19. ohos:multiple_lines="true"
      20. ohos:width="match_parent"
      21. ohos:text_alignment="center|left"
      22. ohos:text_size="20fp"
      23. ohos:text_color="#ed6262"
      24. />
      25. DirectionalLayout>

      4.4实现点击按钮事件,然后存读数据,代码如下

      1. findComponentById(ResourceTable.Id_bt1).setClickedListener(new Component.ClickedListener() {
      2. @Override
      3. public void onClick(Component component) {
      4. List list = new ArrayList<>();
      5. A a = new A(1, 2, 3);
      6. A a2 = new A(2, 2, 3);
      7. A a3 = new A(3, 2, 3);
      8. list.add(a);
      9. list.add(a2);
      10. list.add(a3);
      11. B b = new B(list);
      12. Parcel parcel = Parcel.create();
      13. parcel.writeSequenceable(b);
      14. B b1 = new B();
      15. parcel.readSequenceable(b1);
      16. StringBuilder stringBuilder=new StringBuilder();
      17. if (b1.list.size() > 0) {
      18. for (int i = 0; i < b1.list.size(); i++) {
      19. stringBuilder.append(" b1.list====>" + b1.list.get(i).toString());
      20. stringBuilder.append("\n");
      21. }
      22. }
      23. mResult.setText(stringBuilder.toString());
      24. }
      25. });

    5. 运行效果
      全部代码如下
      5.1xml

      1. xmlns:ohos="http://schemas.huawei.com/res/ohos"
      2. ohos:height="match_parent"
      3. ohos:width="match_parent"
      4. ohos:orientation="vertical">
      5. ohos:id="$+id:bt1"
      6. ohos:height="100vp"
      7. ohos:background_element="#Ed6262"
      8. ohos:width="match_parent"
      9. ohos:text_alignment="center"
      10. ohos:text_size="30fp"
      11. ohos:text_color="#ffffff"
      12. ohos:text="存储数据"/>
      13. ohos:top_margin="10vp"
      14. ohos:id="$+id:Result"
      15. ohos:height="100vp"
      16. ohos:multiple_lines="true"
      17. ohos:width="match_parent"
      18. ohos:text_alignment="center|left"
      19. ohos:text_size="20fp"
      20. ohos:text_color="#ed6262"
      21. />

      5.2 java 代码

      1. package com.harmony.alliance.mydemo.slice;
      2. import com.harmony.alliance.mydemo.ResourceTable;
      3. import ohos.aafwk.ability.AbilitySlice;
      4. import ohos.aafwk.content.Intent;
      5. import ohos.agp.components.Component;
      6. import ohos.agp.components.Text;
      7. import ohos.utils.Parcel;
      8. import ohos.utils.Sequenceable;
      9. import java.util.*;
      10. public class ParcelTestSlice extends AbilitySlice {
      11. private Text mResult;
      12. @Override
      13. protected void onStart(Intent intent) {
      14. super.onStart(intent);
      15. setUIContent(ResourceTable.Layout_slice_main2);
      16. mResult= (Text) findComponentById(ResourceTable.Id_Result);
      17. findComponentById(ResourceTable.Id_bt1).setClickedListener(new Component.ClickedListener() {
      18. @Override
      19. public void onClick(Component component) {
      20. List list = new ArrayList<>();
      21. A a = new A(1, 2, 3);
      22. A a2 = new A(2, 2, 3);
      23. A a3 = new A(3, 2, 3);
      24. list.add(a);
      25. list.add(a2);
      26. list.add(a3);
      27. B b = new B(list);
      28. Parcel parcel = Parcel.create();
      29. parcel.writeSequenceable(b);
      30. B b1 = new B();
      31. parcel.readSequenceable(b1);
      32. StringBuilder stringBuilder=new StringBuilder();
      33. if (b1.list.size() > 0) {
      34. for (int i = 0; i < b1.list.size(); i++) {
      35. stringBuilder.append(" b1.list====>" + b1.list.get(i).toString());
      36. stringBuilder.append("\n");
      37. }
      38. }
      39. mResult.setText(stringBuilder.toString());
      40. }
      41. });
      42. }
      43. public static class B implements Sequenceable {
      44. private List list;
      45. private B() {
      46. }
      47. private B(List list) {
      48. this.list = list;
      49. }
      50. @Override
      51. public boolean hasFileDescriptor() {
      52. return false;
      53. }
      54. @Override
      55. public boolean marshalling(Parcel out) {
      56. out.writeSequenceableList(list);
      57. return true;
      58. }
      59. @Override
      60. public boolean unmarshalling(Parcel in) {
      61. in.addAppClassLoader(getClass().getClassLoader());
      62. list = in.readSequenceableList(A.class);
      63. return true;
      64. }
      65. public static final Sequenceable.Producer PRODUCER = new Sequenceable.Producer() {
      66. public B createFromParcel(Parcel in) {
      67. B instance = new B();
      68. instance.unmarshalling(in);
      69. return instance;
      70. }
      71. };
      72. @Override
      73. public boolean equals(Object o) {
      74. if (this == o) return true;
      75. if (o == null || getClass() != o.getClass()) return false;
      76. B b = (B) o;
      77. return Objects.equals(list, b.list);
      78. }
      79. @Override
      80. public int hashCode() {
      81. return Objects.hash(list);
      82. }
      83. }
      84. public static class A implements Sequenceable {
      85. private int a;
      86. private int b;
      87. private int c;
      88. public A() {
      89. this(0, 0, 0);
      90. }
      91. public A(int a, int b, int c) {
      92. this.a = a;
      93. this.b = b;
      94. this.c = c;
      95. }
      96. @Override
      97. public boolean hasFileDescriptor() {
      98. return false;
      99. }
      100. @Override
      101. public boolean marshalling(Parcel out) {
      102. out.writeInt(a);
      103. out.writeInt(b);
      104. out.writeInt(c);
      105. return true;
      106. }
      107. @Override
      108. public boolean unmarshalling(Parcel in) {
      109. a = in.readInt();
      110. b = in.readInt();
      111. c = in.readInt();
      112. return true;
      113. }
      114. public static final Sequenceable.Producer PRODUCER = new Sequenceable.Producer() {
      115. public A createFromParcel(Parcel in) {
      116. A instance = new A();
      117. instance.unmarshalling(in);
      118. return instance;
      119. }
      120. };
      121. @Override
      122. public boolean equals(Object o) {
      123. if (this == o) return true;
      124. if (o == null || getClass() != o.getClass()) return false;
      125. A a1 = (A) o;
      126. return a == a1.a &&
      127. b == a1.b &&
      128. c == a1.c;
      129. }
      130. @Override
      131. public int hashCode() {
      132. return Objects.hash(a, b, c);
      133. }
      134. @Override
      135. public String toString() {
      136. return "A{" +
      137. "a=" + a +
      138. ", b=" + b +
      139. ", c=" + c +
      140. '}';
      141. }
      142. }
      143. }

      5.3效果如下

    更多相关学习资料:
    https://developer.huawei.com/consumer/cn/forum/topic/0201785060053960095?fid=0101562279236410779?ha_source=zzh

  • 相关阅读:
    WebDAV之葫芦儿·派盘+静读天下
    什么是数字化存在?数字化转型要先从数字化存在开始
    BloomFilter:布隆过滤器和Redis缓存穿透
    【黄啊码】用PHP7性能居然是5.6的三倍?赶紧看看它有什么新特性-续
    CVPR2022 | 长期行动预期的Future Transformer
    Opera 浏览器
    gitlab之cicd的gitlab-runner集成-dockerfile构建环境
    基于JAVA临沂旅游咨询系统计算机毕业设计源码+数据库+lw文档+系统+部署
    基于 SpringBoot 的大学生体质测试管理系统,附源码
    P1039 [NOIP2003 提高组] 侦探推理
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/125807237
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号