OpenHarmony中也有类似于Android的SharedPreferences的轻量级存储,名字叫dataPreferences,这里省略介绍,直接放工具类
首先在EntryAbility中对dataPreferences进行初始化操作,初始化的代码如下:
import dataPreferences from '@ohos.data.preferences';
export var preferences: dataPreferences.Preferences | null = null;
/**
* 获取preferences实例
*/
initPreferenceData() {
try {
dataPreferences.getPreferences(this.context, 'mystore', function (err, val) {
if (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
return;
}
preferences = val;
console.info("Succeeded in getting preferences.");
})
} catch (err) {
console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message);
}
}
将上述的initPreferen