DEV Community

桃花镇童长老
桃花镇童长老

Posted on

PreferencesUtil, Preferences Tool Class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


init initialization
 PreferencesUtil.init("my_app_preferences");
Enter fullscreen mode Exit fullscreen mode
put caches data
PreferencesUtil.put("id", 10000018);
PreferencesUtil.putSync("name", "张三叁");
PreferencesUtil.putSync("bl_nuv", false);
Enter fullscreen mode Exit fullscreen mode
get get cached value
 let id = await PreferencesUtil.get("id", "");
Enter fullscreen mode Exit fullscreen mode
getString Gets the cached value of type string
let name1 = await PreferencesUtil.getString("name");
let name2 = PreferencesUtil.getStringSync('name');
Enter fullscreen mode Exit fullscreen mode
getNumber Gets the cached value of type number
let id1 = await PreferencesUtil.getNumber("id");
let id2 = PreferencesUtil.getNumberSync('id');
Enter fullscreen mode Exit fullscreen mode
getBoolean Gets the cached value of boolean type
let bl1 = await PreferencesUtil.getBoolean("bl_nut");
let bl2 = PreferencesUtil.getBooleanSync('bl_nuv');
Enter fullscreen mode Exit fullscreen mode
has Check whether the cache instance contains the stored key-value pair of the given key
let bl1 = await PreferencesUtil.has("id");
let bl2 = PreferencesUtil.hasSync('name');
Enter fullscreen mode Exit fullscreen mode
delete delete cached value
PreferencesUtil.delete("id");
PreferencesUtil.deleteSync("name");
Enter fullscreen mode Exit fullscreen mode
clear clear cache
PreferencesUtil.clear();
PreferencesUtil.clearSync();
Enter fullscreen mode Exit fullscreen mode
deletePreferences Removes the specified Preferences instance from the cache. If the Preferences instance has a corresponding persistent file, its persistent file will be deleted at the same time.
 PreferencesUtil.deletePreferences(getContext(), "SP_PREFERENCES_MSG");
Enter fullscreen mode Exit fullscreen mode
onChange Subscribe to data change. After the value of the subscribed Key is changed, the callback callback is triggered after the flush method is executed.
  private callback: Callback<string> = (value) => {
    LogUtil.error("onChange: " + value);
  }

 PreferencesUtil.onChange(callback);
Enter fullscreen mode Exit fullscreen mode
offChange Unsubscribe to data changes
 PreferencesUtil.offChange(callback);
Enter fullscreen mode Exit fullscreen mode
onDataChange precisely subscribes to data changes. Only after the subscribed key value changes, the callback callback is triggered after the flush method is executed.
  private dataCallback = (data: Record<string, preferences.ValueType>) => {
    for (const keyValue of Object.entries(data)) {
      LogUtil.error("onDataChange: " + keyValue);
    }
  }

  PreferencesUtil.onDataChange(["id", "name"], dataCallback);
Enter fullscreen mode Exit fullscreen mode
offDataChange Cancel the exact subscription data change
PreferencesUtil.offDataChange(["id", "name"], dataCallback);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)