HarmonyOS鸿蒙开发_Preferences持久化存储

1.新建util文件夹,新建PreferencesUtil.ets


/*

* Copyright (c) 2024 Huawei Device Co., Ltd.

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

*    http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

import { preferences } from '@kit.ArkData';

import { BusinessError } from '@kit.BasicServicesKit';

import { hilog } from '@kit.PerformanceAnalysisKit';

const KEY_APP_FONT_SIZE = 'appFontSize';

const TAG = 'PreferencesUtil';

/**

* The PreferencesUtil provides preferences of create, save and query.

*/

export class PreferencesUtil {

  preference?: preferences.Preferences;

  /**

*The method of creating a preferences Instance.

*/

  getFontPreferences(context: Context) {

    this.preference = preferences.getPreferencesSync(context, { name: 'FontPreferences' });

    hilog.info(0x0000, TAG, 'create success');

  }

  /**

*The method of saving fontsize offset change.

*/

  saveChangeFontSize(fontSizeOffset: number) {

    this.preference?.putSync(KEY_APP_FONT_SIZE, fontSizeOffset);

    this.preference?.flush(

      (err: BusinessError) => {

        if (err) {

          hilog.error(0x0000, TAG, 'Failed to flush. code =' + err.code + ', message =' + err.message);

          return;

        }

        hilog.info(0x0000, TAG, 'Succeeded in flushing.');

      });

  }

  /**

*The method of getting fontsize offset.

*/

  getChangeFontSize() {

    let fontSizeOffset: number = 0;

    fontSizeOffset = this.preference?.getSync(KEY_APP_FONT_SIZE, 0) as number;

    return fontSizeOffset;

  }

  /**

*The method of Determining Whether a key Exists.

*/

  isKeyExist(): boolean {

    let isKeyExist: boolean = false;

    this.preference?.has(KEY_APP_FONT_SIZE).then(async (isExist: boolean) => {

      isKeyExist = isExist;

    }).catch((err: Error) => {

      hilog.error(0x0000, TAG, 'Has the value failed with err: ' + err);

    });

    return isKeyExist;

  }

}

export default new PreferencesUtil();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容