赞
踩
申请权限
- WebConfig webConfig = webView.getWebConfig();
- // 是否可以访问本地文件,默认值 true
- webConfig.setJavaScriptPermit(true);
设置JsCallback
- webView.addJsCallback("getToken",
- str -> PreferenceUtils.getString(null, ConfigContants.token)
- );
包引用
implementation 'com.zzrv5.zzrhttp:ZZRHttp:1.0.1'
请求代码
- ZZRHttp.get(Apis.FA_API, new ZZRCallBack.CallBackString() {
-
- @Override
- public void onFailure(int code, String json) {
- HiLogUtil.error("请求失败 code=%{public}d result=%{public}s", code, json);
- webView.load(PreferenceUtils.getString(null, ConfigContants.mainHome));
- }
-
- @Override
- public void onResponse(String json) {
- HiLogUtil.info("请求成功 result=%{public}s", json);
- Gson gson = new Gson();
- try {
- Result<String> result = gson.fromJson(json,
- new TypeToken<Result<String>>(){}.getType());
- if ("200".equals(result.getCode())) {
- String data = result.getData();
- Map<String, String> config = gson.fromJson(data,
- new TypeToken<Map<String,String>>(){}.getType());
- webView.load(config.get("url"));
- PreferenceUtils.putString(null,
- ConfigContants.mainHome, config.get("url"));
- } else {
- webView.load(PreferenceUtils.getString(null,
- ConfigContants.mainHome));
- }
- } catch (Exception e) {
- webView.load(PreferenceUtils.getString(null,
- ConfigContants.mainHome));
- }
- }
- });

应用交互
数据存储
- package com.x2era.hmos.util;
-
- import com.x2era.hmos.base.ConfigContants;
- import ohos.app.Context;
- import ohos.data.DatabaseHelper;
- import ohos.data.preferences.Preferences;
-
- import java.util.Set;
-
- public class PreferenceUtils {
-
- private static Preferences preferences;
- private static DatabaseHelper databaseHelper;
- private static Preferences.PreferencesObserver mPreferencesObserver;
-
- private static synchronized void initPreference(Context context){
- if(databaseHelper == null){
- databaseHelper = new DatabaseHelper(context);
- }
- if(preferences == null){
- preferences = databaseHelper.getPreferences(ConfigContants.settingConf);
- }
- }
-
- //存放、获取时传入的context必须是同一个context,否则存入的数据无法获取
- public static void putString(Context context, String key, String value) {
- initPreference(context);
- preferences.putString(key, value);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取的String 默认值为:null
- */
- public static String getString(Context context, String key) {
- initPreference(context);
- return preferences.getString(key, null);
- }
-
-
- public static void putInt(Context context, String key, int value) {
- initPreference(context);
- preferences.putInt(key, value);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取int的默认值为:-1
- */
- public static int getInt(Context context, String key) {
- initPreference(context);
- return preferences.getInt(key, -1);
- }
-
-
- public static void putLong(Context context, String key, long value) {
- initPreference(context);
- preferences.putLong(key, value);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取long的默认值为:-1
- */
- public static long getLong(Context context, String key) {
- initPreference(context);
- return preferences.getLong(key, -1L);
- }
-
-
- public static void putBoolean(Context context, String key, boolean value) {
- initPreference(context);
- preferences.putBoolean(key, value);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取boolean的默认值为:false
- */
- public static boolean getBoolean(Context context, String key) {
- initPreference(context);
- return preferences.getBoolean(key, false);
- }
-
-
- public static void putFloat(Context context, String key, float value) {
- initPreference(context);
- preferences.putFloat(key, value);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取float的默认值为:0.0
- */
- public static float getFloat(Context context, String key) {
- initPreference(context);
- return preferences.getFloat(key, 0.0F);
- }
-
-
- public static void putStringSet(Context context, String key, Set<String> set) {
- initPreference(context);
- preferences.putStringSet(key, set);
- preferences.flushSync();
- }
-
- /**
- * @param context 上下文
- * @param key 键
- * @return 获取set集合的默认值为:null
- */
- public static Set<String> getStringSet(Context context, String key) {
- initPreference(context);
- return preferences.getStringSet(key, null);
- }
-
-
- public static boolean deletePreferences(Context context) {
- initPreference(context);
- boolean isDelete= databaseHelper.deletePreferences(ConfigContants.settingConf);
- return isDelete;
- }
-
-
- public static void registerObserver(Context context, Preferences.PreferencesObserver preferencesObserver){
- initPreference(context);
- mPreferencesObserver=preferencesObserver;
- preferences.registerObserver(mPreferencesObserver);
- }
-
- public static void unregisterObserver(){
- if(mPreferencesObserver!=null){
- // 向preferences实例注销观察者
- preferences.unregisterObserver(mPreferencesObserver);
- }
- }
-
- }

卡片镜像配置,规格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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。