diff --git a/README.en.md b/README.en.md index e6d1952e88d6d0022b3fe47f5d50df45fc95db8d..e3a2c17266a3a68659e5b79d0dbc4001cd9956f2 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 3f0b5048dffaae232037c2614c84898b0febe269..1a094e803ec1981646ae96b5d7471e95305be4ad 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 68f481c965afe46cfa2e8835779cc1b5e27e0356..49dc4d0e6570b8110f64965c08bfa3c3724433b7 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" } ] diff --git a/entry/src/main/ets/entryability/EntryAbility.ts b/entry/src/main/ets/entryability/EntryAbility.ts index 6d0226717e01a87efc562299818fb65243cdd3c9..3d7bba35ce047df9c0e4633e5d14744981a7363e 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 c737f4338b8f573f59a6f2f4d0e2930b92eaef3c..23e1149e3d08157f7a97f05768cbd873de6ce2f6 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