From 2b0bea9cbb42d7c12e6cf295bc1777bf4288577f Mon Sep 17 00:00:00 2001 From: WangLin305 Date: Fri, 22 Aug 2025 15:58:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 6 +++--- README.md | 6 +++--- build-profile.json5 | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.en.md b/README.en.md index e6d1952..e3a2c17 100644 --- a/README.en.md +++ b/README.en.md @@ -26,6 +26,6 @@ N/A ### Constraints 1. The sample app is supported only on Huawei phones running the standard system. -2. HarmonyOS: HarmonyOS 5.0.0 Release or later -3. DevEco Studio: DevEco Studio 5.0.0 Release or later -4. HarmonyOS SDK: HarmonyOS 5.0.0 Release SDK or later +2. HarmonyOS: HarmonyOS 5.0.5 Release or later +3. DevEco Studio: DevEco Studio 5.0.5 Release or later +4. HarmonyOS SDK: HarmonyOS 5.0.5 Release SDK or later diff --git a/README.md b/README.md index 3f0b504..1a094e8 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,6 @@ ### 约束与限制 1. 本示例仅支持标准系统上运行,支持设备:华为手机。 -2. HarmonyOS系统:HarmonyOS 5.0.0 Release及以上。 -3. DevEco Studio版本:DevEco Studio 5.0.0 Release及以上。 -4. HarmonyOS SDK版本:HarmonyOS 5.0.0 Release SDK及以上。 +2. HarmonyOS系统:HarmonyOS 5.0.5 Release及以上。 +3. DevEco Studio版本:DevEco Studio 5.0.5 Release及以上。 +4. HarmonyOS SDK版本:HarmonyOS 5.0.5 Release SDK及以上。 diff --git a/build-profile.json5 b/build-profile.json5 index 68f481c..49dc4d0 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -5,8 +5,8 @@ { "name": "default", "signingConfig": "default", - "compatibleSdkVersion": "5.0.0(12)", - "targetSdkVersion": "5.0.0(12)", + "targetSdkVersion": "5.0.5(17)", + "compatibleSdkVersion": "5.0.5(17)", "runtimeOS": "HarmonyOS" } ] -- Gitee From 9aed88fdad37f0013ad8c551bb788bcd120a5ab7 Mon Sep 17 00:00:00 2001 From: WangLin305 Date: Fri, 19 Sep 2025 13:56:23 +0800 Subject: [PATCH 2/2] catch throw error --- .../src/main/ets/entryability/EntryAbility.ts | 6 +++ entry/src/main/ets/model/PreferenceModel.ets | 45 ++++++++++++------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index 6d02267..3d7bba3 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ts +++ b/entry/src/main/ets/entryability/EntryAbility.ts @@ -44,6 +44,12 @@ export default class EntryAbility extends UIAbility { hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); + + try { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + } catch (error) { + hilog.error(0x0000, 'testTag', `getMainWindowSync catch error, code: ${error.code}, message: ${error.message}`); + } } onWindowStageDestroy(): void { diff --git a/entry/src/main/ets/model/PreferenceModel.ets b/entry/src/main/ets/model/PreferenceModel.ets index c737f43..23e1149 100644 --- a/entry/src/main/ets/model/PreferenceModel.ets +++ b/entry/src/main/ets/model/PreferenceModel.ets @@ -15,11 +15,13 @@ import { promptAction } from '@kit.ArkUI'; import { preferences } from '@kit.ArkData'; +import { common } from '@kit.AbilityKit'; import Logger from '../common/utils/Logger'; import CommonConstants from '../common/constants/CommonConstants'; import Fruit from '../viewmodel/Fruit'; -let context = getContext(this); +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +let context = uiContext!.getHostContext() as common.UIAbilityContext; let preference: preferences.Preferences; let preferenceTemp: preferences.Preferences; @@ -48,9 +50,10 @@ class PreferenceModel { async deletePreferences() { try { await preferences.deletePreferences(context, CommonConstants.PREFERENCES_NAME); - } catch(err) { + } catch (err) { Logger.error(CommonConstants.TAG, `Failed to delete preferences, Cause: ${err}`); - }; + } + ; preference = preferenceTemp; this.showToastMessage($r('app.string.delete_success_msg')); } @@ -61,17 +64,21 @@ class PreferenceModel { * @param fruit Fruit data. */ async putPreference(fruit: Fruit) { - if (!preference) { - await this.getPreferencesFromStorage(); - } - // The fruit name and fruit quantity data entered by the user are saved to the cached Preference instance. try { - await preference.put(CommonConstants.KEY_NAME, JSON.stringify(fruit)); - } catch (err) { - Logger.error(CommonConstants.TAG, `Failed to put value, Cause: ${err}`); + if (!preference) { + await this.getPreferencesFromStorage(); + } + // The fruit name and fruit quantity data entered by the user are saved to the cached Preference instance. + try { + await preference.put(CommonConstants.KEY_NAME, JSON.stringify(fruit)); + } catch (err) { + Logger.error(CommonConstants.TAG, `Failed to put value, Cause: ${err}`); + } + // Store the Preference instance in the preference persistence file + await preference.flush(); + } catch (error) { + Logger.error(`getMainWindowSync catch error, code: ${error.code}, message: ${error.message}`); } - // Store the Preference instance in the preference persistence file - await preference.flush(); } /** @@ -140,11 +147,15 @@ class PreferenceModel { * @param message Prompt message. */ showToastMessage(message: Resource) { - promptAction.showToast({ - message: message, - duration: CommonConstants.DURATION - }); - }; + try { + uiContext!.getPromptAction().showToast({ + message: message, + duration: CommonConstants.DURATION + }); + } catch (error) { + Logger.error(`showToast catch error, code: ${error.code}, message: ${error.message}`); + } + } } export default new PreferenceModel(); \ No newline at end of file -- Gitee