From bc612b8c02a2a4f5e0249f142992cf01b51b85e1 Mon Sep 17 00:00:00 2001 From: haoliang <2100448934@qq.com> Date: Wed, 14 Sep 2022 14:51:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: haoliang <2100448934@qq.com> --- product/phone/src/main/ets/MainAbility/MainAbility.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/product/phone/src/main/ets/MainAbility/MainAbility.ts b/product/phone/src/main/ets/MainAbility/MainAbility.ts index db4f5c4e..20a44a4a 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -34,7 +34,6 @@ export default class MainAbility extends Ability { LogUtil.log("[Main] MainAbility onWindowStageCreate") windowStage.setUIContent(this.context, "pages/settingList", null) - globalThis.settingsAbilityContext =this.context } onWindowStageDestroy() { -- Gitee From fa6971eddaf49bda46627779a45fd44989d9c9ee Mon Sep 17 00:00:00 2001 From: haoliang <455407979@qq.com> Date: Thu, 15 Sep 2022 09:58:09 +0800 Subject: [PATCH 2/2] update code Signed-off-by: haoliang <455407979@qq.com> --- .../main/ets/default/baseUtil/ConfigData.ts | 1 + .../ets/default/baseUtil/PreferenceUtil.ts | 60 +++++++++++-------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/common/utils/src/main/ets/default/baseUtil/ConfigData.ts b/common/utils/src/main/ets/default/baseUtil/ConfigData.ts index 1e53ed84..c357da49 100644 --- a/common/utils/src/main/ets/default/baseUtil/ConfigData.ts +++ b/common/utils/src/main/ets/default/baseUtil/ConfigData.ts @@ -19,6 +19,7 @@ export class ConfigData { FILE_URI = '/data/accounts/account_0/applications/com.ohos.settings' + '/com.ohos.settings/assets/phone/resources/rawfile/'; PREFERENCES_PATH = '/data/accounts/account_0/appdata/com.ohos.settings/sharedPreference/SettingPreferences'; + PREFERENCES_NAME = 'SettingPreferences'; BRIGHTNESS_SAVE_VALUE_KEY = 'BrightnessSaveValue'; SENT_EVENT_BROADCAST_BRIGHTNESS_VALUE = 'BRIGHTNESS_VALUE'; diff --git a/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts b/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts index c8ceec73..c7662167 100644 --- a/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts +++ b/common/utils/src/main/ets/default/baseUtil/PreferenceUtil.ts @@ -13,38 +13,46 @@ * limitations under the License. */ -import ConfigData from './ConfigData'; +import ConfigData from './ConfigData' import LogUtil from './LogUtil' -//import Storage from '@ohos.data.storage'; +import Storage from '@ohos.data.storage' -//let preference = Storage.getStorageSync(ConfigData.PREFERENCES_PATH); -this.storage = await dataStorage.getPreferences(globalThis.settingsAbilityContext, ConfigData.PREFERENCES_PATH) export class PreferenceUtil { - /** - * Set up storage data - * - * @param key - key - * @param value - value - */ - setStorageValue(key,value): void { - LogUtil.info(`Set preference, key: ${key}, value: ${value}")`); - preference.putSync(key, value); - preference.flushSync(); - } + private preference: Storage.Storage - /** - * Get stored data - * - * @param key - key - * @param defaultValue - defaultValue - */ - getStorageValue(key: string, defaultValue) { - let value = preference.getSync(key, defaultValue); - LogUtil.info(`Get storage value, key: ${key}, value: ${value}`); - return value; - } + constructor() { + if (!this.preference) { + Storage.getStorage(globalThis.settingsAbilityContext.filesDir + '/' + ConfigData.PREFERENCES_NAME).then(data => { + this.preference = data + }) + } + } + /** + * Set up storage data + * + * @param key - key + * @param value - value + */ + setStorageValue(key, value): void { + LogUtil.info(`Set preference, key: ${key}, value: ${value}")`); + this.preference.putSync(key, value); + this.preference.flushSync(); + } + + /** + * Get stored data + * + * @param key - key + * @param defaultValue - defaultValue + */ + getStorageValue(key: string, defaultValue) { + let value = this.preference.getSync(key, defaultValue); + LogUtil.info(`Get storage value, key: ${key}, value: ${value}`); + return value; + } } let preferenceUtil = new PreferenceUtil(); + export default preferenceUtil as PreferenceUtil \ No newline at end of file -- Gitee