当前位置:   article > 正文

鸿蒙服务卡片开发总结_鸿蒙 java 桌面卡片实现demo

鸿蒙 java 桌面卡片实现demo

​工程目录

外部H5进行数据交互

  • 申请权限

  1. WebConfig webConfig = webView.getWebConfig();
  2. // 是否可以访问本地文件,默认值 true
  3. webConfig.setJavaScriptPermit(true);
  • 设置JsCallback

  1. webView.addJsCallback("getToken",
  2. str -> PreferenceUtils.getString(null, ConfigContants.token)
  3. );

Http 接口调用

  • 包引用

implementation 'com.zzrv5.zzrhttp:ZZRHttp:1.0.1'
  • 请求代码

  1. ZZRHttp.get(Apis.FA_API, new ZZRCallBack.CallBackString() {
  2. @Override
  3. public void onFailure(int code, String json) {
  4. HiLogUtil.error("请求失败 code=%{public}d result=%{public}s", code, json);
  5. webView.load(PreferenceUtils.getString(null, ConfigContants.mainHome));
  6. }
  7. @Override
  8. public void onResponse(String json) {
  9. HiLogUtil.info("请求成功 result=%{public}s", json);
  10. Gson gson = new Gson();
  11. try {
  12. Result<String> result = gson.fromJson(json,
  13. new TypeToken<Result<String>>(){}.getType());
  14. if ("200".equals(result.getCode())) {
  15. String data = result.getData();
  16. Map<String, String> config = gson.fromJson(data,
  17. new TypeToken<Map<String,String>>(){}.getType());
  18. webView.load(config.get("url"));
  19. PreferenceUtils.putString(null,
  20. ConfigContants.mainHome, config.get("url"));
  21. } else {
  22. webView.load(PreferenceUtils.getString(null,
  23. ConfigContants.mainHome));
  24. }
  25. } catch (Exception e) {
  26. webView.load(PreferenceUtils.getString(null,
  27. ConfigContants.mainHome));
  28. }
  29. }
  30. });

工具类

  • 应用交互

  • 数据存储

  1. package com.x2era.hmos.util;
  2. import com.x2era.hmos.base.ConfigContants;
  3. import ohos.app.Context;
  4. import ohos.data.DatabaseHelper;
  5. import ohos.data.preferences.Preferences;
  6. import java.util.Set;
  7. public class PreferenceUtils {
  8. private static Preferences preferences;
  9. private static DatabaseHelper databaseHelper;
  10. private static Preferences.PreferencesObserver mPreferencesObserver;
  11. private static synchronized void initPreference(Context context){
  12. if(databaseHelper == null){
  13. databaseHelper = new DatabaseHelper(context);
  14. }
  15. if(preferences == null){
  16. preferences = databaseHelper.getPreferences(ConfigContants.settingConf);
  17. }
  18. }
  19. //存放、获取时传入的context必须是同一个context,否则存入的数据无法获取
  20. public static void putString(Context context, String key, String value) {
  21. initPreference(context);
  22. preferences.putString(key, value);
  23. preferences.flushSync();
  24. }
  25. /**
  26. * @param context 上下文
  27. * @param key 键
  28. * @return 获取的String 默认值为:null
  29. */
  30. public static String getString(Context context, String key) {
  31. initPreference(context);
  32. return preferences.getString(key, null);
  33. }
  34. public static void putInt(Context context, String key, int value) {
  35. initPreference(context);
  36. preferences.putInt(key, value);
  37. preferences.flushSync();
  38. }
  39. /**
  40. * @param context 上下文
  41. * @param key 键
  42. * @return 获取int的默认值为:-1
  43. */
  44. public static int getInt(Context context, String key) {
  45. initPreference(context);
  46. return preferences.getInt(key, -1);
  47. }
  48. public static void putLong(Context context, String key, long value) {
  49. initPreference(context);
  50. preferences.putLong(key, value);
  51. preferences.flushSync();
  52. }
  53. /**
  54. * @param context 上下文
  55. * @param key 键
  56. * @return 获取long的默认值为:-1
  57. */
  58. public static long getLong(Context context, String key) {
  59. initPreference(context);
  60. return preferences.getLong(key, -1L);
  61. }
  62. public static void putBoolean(Context context, String key, boolean value) {
  63. initPreference(context);
  64. preferences.putBoolean(key, value);
  65. preferences.flushSync();
  66. }
  67. /**
  68. * @param context 上下文
  69. * @param key 键
  70. * @return 获取boolean的默认值为:false
  71. */
  72. public static boolean getBoolean(Context context, String key) {
  73. initPreference(context);
  74. return preferences.getBoolean(key, false);
  75. }
  76. public static void putFloat(Context context, String key, float value) {
  77. initPreference(context);
  78. preferences.putFloat(key, value);
  79. preferences.flushSync();
  80. }
  81. /**
  82. * @param context 上下文
  83. * @param key 键
  84. * @return 获取float的默认值为:0.0
  85. */
  86. public static float getFloat(Context context, String key) {
  87. initPreference(context);
  88. return preferences.getFloat(key, 0.0F);
  89. }
  90. public static void putStringSet(Context context, String key, Set<String> set) {
  91. initPreference(context);
  92. preferences.putStringSet(key, set);
  93. preferences.flushSync();
  94. }
  95. /**
  96. * @param context 上下文
  97. * @param key 键
  98. * @return 获取set集合的默认值为:null
  99. */
  100. public static Set<String> getStringSet(Context context, String key) {
  101. initPreference(context);
  102. return preferences.getStringSet(key, null);
  103. }
  104. public static boolean deletePreferences(Context context) {
  105. initPreference(context);
  106. boolean isDelete= databaseHelper.deletePreferences(ConfigContants.settingConf);
  107. return isDelete;
  108. }
  109. public static void registerObserver(Context context, Preferences.PreferencesObserver preferencesObserver){
  110. initPreference(context);
  111. mPreferencesObserver=preferencesObserver;
  112. preferences.registerObserver(mPreferencesObserver);
  113. }
  114. public static void unregisterObserver(){
  115. if(mPreferencesObserver!=null){
  116. // 向preferences实例注销观察者
  117. preferences.unregisterObserver(mPreferencesObserver);
  118. }
  119. }
  120. }

注意事项

  • 卡片镜像配置,规格600x600

    • 不放会导致无法在常用服务内展示

  • config.json 配置

    • 2x2卡片的isDefault 必须为true,不然常用服务无法展示

    • launchType 需设置为singleton,standard 在 startAbility无法正常页面跳转

开发者指南

1. 快速注册成为开发者:https://developer.huawei.com/consumer/cn/doc/start/registration-and-verification-0000001053628148
2. 准备开发环境 :https://developer.harmonyos.com/cn/docs/documentation/doc-guides/installation_process-0000001071425528
3. 服务发布:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/publish_app-0000001053223745
4. 卡片设计指南:https://developer.harmonyos.com/cn/docs/design/des-guides/service-widget-about-0000001144696239
5. 卡片开发指南:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ability-service-widget-provider-js-0000001150602175
6. java UI 定位:https://developer.harmonyos.com/cn/docs/documentation/doc-references/location-0000001054358881
7. 动态权限申请:https://developer.harmonyos.com/cn/docs/documentation/doc-references/location-0000001054358881
## 样例代码
1. Bilibili客户端 https://gitee.com/liangzili/bilibili-cards/tree/master/BilibiliCards/entry/src/main/java/com/liangzili/demos

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/221022
推荐阅读
相关标签
  

闽ICP备14008679号