1import preferences from '@ohos.data.preferences' 2 3export class PreferenceUtil { 4 static getStore() { 5 const context = AppStorage.get<Context>('abilityContext') 6 return preferences.getPreferencesSync(context, { 7 name: 'preference_apn' 8 }) 9 } 10 11 static put(key: string, value: preferences.ValueType) { 12 const store = PreferenceUtil.getStore() 13 store.putSync(key, value) 14 store.flush() 15 } 16 17 static get(key: string, defaultValue: preferences.ValueType) { 18 const store = PreferenceUtil.getStore() 19 return store.getSync(key, defaultValue) 20 } 21 22 static delete(key: string) { 23 const store = PreferenceUtil.getStore() 24 store.deleteSync(key) 25 store.flush() 26 } 27}