一、使用方法:
0、初始化实例:一般在EntryAbility.ts的onWindowStageCreate中初始化;(可忽略)
1、将数据写入Preferences实例
function() { let arrayNum: number[] = [1, 2, 3]; let arrayStr: string[] = ["5", "2", "3"]; let arrayBoo: boolean[] = [true, false, true]; var fruit: Fruit; fruit = new Fruit(); fruit.fruitName = "Name"; fruit.fruitNum = "Num"; PFUtils.put("number", 10); PFUtils.put("string", "22"); PFUtils.put("boolean", true); PFUtils.put("arrayNum", arrayNum); PFUtils.put("arrayStr", arrayStr); PFUtils.put("arrayBoo", arrayBoo); PFUtils.put("fruit", fruit) // 自定义实体 this.breakpointSystem.register(); this.intervalId = setInterval(this.countdown, 1000); }
2、获取键对应的值
function() { PFUtils.get("number").then((value: number) => { Logger.error(Constants.TAG, "number:" + value) }) PFUtils.get("string").then((value: string) => { Logger.error(Constants.TAG, "string:" + value) }) PFUtils.get("boolean").then((value: boolean) => { Logger.error(Constants.TAG, "boolean:" + value) }) PFUtils.get("arrayNum").then((value: Array<number>) => { Logger.error(Constants.TAG, "Array<number>:" + value) }) PFUtils.get("arrayStr").then((value: Array<string>) => { Logger.error(Constants.TAG, "Array<string>:" + value) }) PFUtils.get("arrayBoo").then((value: Array<boolean>) => { Logger.error(Constants.TAG, "Array<boolean>:" + value) }) PFUtils.getEntity("fruit").then((value: Fruit) => { Logger.error(Constants.TAG, "Fruit:" + value) Logger.error(Constants.TAG, "FruitName:" + value.fruitName) Logger.error(Constants.TAG, "FruitNum:" + value.fruitNum) }) }
3、其他方法:
1、getAll:获取含有所有键值的Object对象。
2、has:检查Preferences实例是否包含名为给定Key的存储键值对
3、delete:从Preferences实例中删除名为给定Key的存储键值对
4、flush:将当前Preferences实例的数据异步存储到用户首选项的持久化文件中(put中已生效)
5、clear:清除此Preferences实例中的所有存储
二、完整代码:
import dataPreferences from '@ohos.data.preferences'; import { Constants, Logger } from '@ohos/common'; let preference: dataPreferences.Preferences; let preferenceTemp: dataPreferences.Preferences; let context = getContext(this); export class PFUtils { /** * Read the specified Preferences persistence file and load the data into the Preferences instance. */ static async getPreferences(context: Context) { try { preference = await dataPreferences.getPreferences(context, Constants.PREFERENCES_NAME); } catch (err) { Logger.error(Constants.TAG, `Failed to get preferences, Cause: ${err}`); } } /** * Deletes the specified Preferences persistence file from memory and removes the Preferences instance. */ static async deletePreferences() { try { await dataPreferences.deletePreferences(context, Constants.PREFERENCES_NAME); } catch (err) { Logger.error(Constants.TAG, `Failed to delete preferences, Cause: ${err}`); } preference = preferenceTemp; } /** * Save the data to the Preferences. * * @param data. */ static async put(key: string, value: unknown) { if (!preference) { await this.getPreferences(getContext(this)); } try { if (typeof value === "number") { await preference.put(key, value); } if (typeof value === "string") { await preference.put(key, value); } if (typeof value === "boolean") { await preference.put(key, value); } if (typeof value === "object") { if (Array.isArray(value)) { await preference.put(key, value); } else { await preference.put(key, JSON.stringify(value)); } } } catch (err) { Logger.error(Constants.TAG, `Failed to put value, Cause: ${err}`); } // Store the Preference instance in the preference persistence file await preference.flush(); } /** * Get preference data. */ static async get(key: string) { let value: unknown; if (!preference) { await this.getPreferences(getContext(this)); } try { value = await preference.get(key, undefined).then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } return value; } /** * Get custom entity data. */ static async getEntity(key: string) { let value: string = ''; if (!preference) { await this.getPreferences(getContext(this)); } try { value = (await preference.get(key, '')).toString(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } return JSON.parse(value); } /** * Get an Object object that contains all key values. */ static async getAll() { let value: unknown; if (!preference) { await this.getPreferences(getContext(this)); } try { value = await preference.getAll().then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } return value; } /** * Check if the Preferences instance contains a stored key value pair named the given Key */ static async has(key: string) { let value: boolean = false; if (!preference) { await this.getPreferences(getContext(this)); } try { value = await preference.has(key).then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } return value; } /** * Delete a stored key value pair named the given Key from the Preferences instance */ static async delete(key: string) { if (!preference) { await this.getPreferences(getContext(this)); } try { await preference.delete(key).then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } } /** * Clear all storage in this Preferences instance */ static async clear() { if (!preference) { await this.getPreferences(getContext(this)); } try { await preference.clear().then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } } /** *Asynchronous storage of data from the current Preferences instance to the persistent file of user preferences */ static async flush() { if (!preference) { await this.getPreferences(getContext(this)); } try { await preference.flush().then(); } catch (err) { Logger.error(Constants.TAG, `Failed to get value, Cause: ${err}`); } } }