js-apis-data-preferences.md 28.3 KB
Newer Older
W
wangxiyue 已提交
1
# 首选项
P
PaDoBoo 已提交
2

W
wangxiyue 已提交
3
首选项为应用提供key-value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型。
P
PaDoBoo 已提交
4 5 6


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
7
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
P
PaDoBoo 已提交
8 9 10 11


## 导入模块

W
wangxiyue 已提交
12
```ts
G
ge-yafang 已提交
13
import data_preferences from '@ohos.data.preferences';
P
PaDoBoo 已提交
14 15
```

G
ge-yafang 已提交
16
## 常量
P
PaDoBoo 已提交
17

18
**系统能力**:以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core
19

P
PaDoBoo 已提交
20 21
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
22
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 |
G
ge-yafang 已提交
23
| MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,大小为8192字节。 |
P
PaDoBoo 已提交
24 25


P
PaDoBoo 已提交
26
## data_preferences.getPreferences
P
PaDoBoo 已提交
27

P
PaDaBoo 已提交
28
getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void
P
PaDoBoo 已提交
29

W
wangxiyue 已提交
30
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
31

P
PaDaBoo 已提交
32

P
PaDaBoo 已提交
33
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
34

G
ge-yafang 已提交
35
**参数:**
P
PaDoBoo 已提交
36 37
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
38
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDoBoo 已提交
39 40 41
  | name | string | 是 | 应用程序内部数据存储名称。 |
  | callback | AsyncCallback<[Preferences](#preferences)> | 是 | 回调函数。 |

G
ge-yafang 已提交
42
**示例:**
W
wangxiyue 已提交
43
  ```ts
P
PaDoBoo 已提交
44
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
45
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
46
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
47
  
P
PaDaBoo 已提交
48
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
49
          if (err) {
P
PaDaBoo 已提交
50 51
              console.info("Get the preferences failed")
              return;
P
PaDoBoo 已提交
52
          }
P
PaDaBoo 已提交
53
          preferences.put('startup', 'auto', function (err) {
P
PaDoBoo 已提交
54
              if (err) {
P
PaDaBoo 已提交
55
                  console.info("Put the value of startup failed, err: " + err)
P
PaDoBoo 已提交
56 57
                  return
              }
P
PaDaBoo 已提交
58 59 60 61 62 63 64 65
              console.info("Put the value of startup successfully.")
              preferences.flush(function (err) {
                  if (err) {
                      console.info("Flush to file failed, err: " + err)
                      return
                  }
                  console.info("Flushed to file successfully.")
              })
P
PaDoBoo 已提交
66 67
          })
      })
P
PaDaBoo 已提交
68
  }
P
PaDoBoo 已提交
69 70 71
  ```


P
PaDoBoo 已提交
72
## data_preferences.getPreferences
P
PaDoBoo 已提交
73

P
PaDaBoo 已提交
74
getPreferences(context: Context, name: string): Promise<Preferences>
P
PaDoBoo 已提交
75

W
wangxiyue 已提交
76
读取指定首选项持久化文件,将数据加载到Preferences实例,用于数据操作,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
77

P
PaDaBoo 已提交
78
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
79

G
ge-yafang 已提交
80
**参数:**
P
PaDoBoo 已提交
81 82
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
83
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDoBoo 已提交
84 85
  | name | string | 是 | 应用程序内部数据存储名称。 |

G
ge-yafang 已提交
86
**返回值:**
P
PaDoBoo 已提交
87 88 89 90
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
91
**示例:**
W
wangxiyue 已提交
92
  ```ts
P
PaDaBoo 已提交
93
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
94
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
95
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
96
  
P
PaDaBoo 已提交
97 98 99
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          preferences.put('startup', 'auto', function (err) {
P
PaDoBoo 已提交
100
              if (err) {
P
PaDaBoo 已提交
101
                  console.info("Put the value of startup failed, err: " + err)
P
PaDoBoo 已提交
102 103
                  return
              }
P
PaDaBoo 已提交
104 105 106 107 108 109 110 111
              console.info("Put the value of startup successfully.")
              preferences.flush(function (err) {
                  if (err) {
                      console.info("Flush to file failed, err: " + err)
                      return
                  }
                  console.info("Flushed to file successfully.")
              })
P
PaDoBoo 已提交
112
          })
P
PaDaBoo 已提交
113 114
      }).catch((err) => {
          console.info("Get the preferences failed")
P
PaDoBoo 已提交
115
      })
P
PaDaBoo 已提交
116
  }
P
PaDoBoo 已提交
117 118 119
  ```


P
PaDoBoo 已提交
120
## data_preferences.deletePreferences
P
PaDoBoo 已提交
121

122
deletePreferences(context: Context, name: string, callback: AsyncCallback<void>): void
P
PaDoBoo 已提交
123

W
wangxiyue 已提交
124 125
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。
删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
126

P
PaDaBoo 已提交
127
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
128

G
ge-yafang 已提交
129
**参数:**
P
PaDoBoo 已提交
130 131
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
132
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
133
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
134 135
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
136
**示例:**
W
wangxiyue 已提交
137
  ```ts
P
PaDaBoo 已提交
138
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
139
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
140
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
141
  
P
PaDaBoo 已提交
142 143 144 145 146 147 148 149
      data_preferences.deletePreferences(this.context, 'mystore', function (err) {
          if (err) {
              console.info("Deleted failed, err: " + err)
              return
          }
          console.info("Deleted successfully.")
      })
  }
P
PaDoBoo 已提交
150 151 152
  ```


P
PaDoBoo 已提交
153
## data_preferences.deletePreferences
P
PaDoBoo 已提交
154

P
PaDaBoo 已提交
155
deletePreferences(context: Context, name: string): Promise<void>
P
PaDoBoo 已提交
156

W
wangxiyue 已提交
157 158
从内存中移除指定首选项持久化文件对应的Preferences单实例,并删除指定文件及其备份文件和损坏文件。
删除指定首选项持久化文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
159

P
PaDaBoo 已提交
160
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
161

G
ge-yafang 已提交
162
**参数:**
P
PaDoBoo 已提交
163 164
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
165
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
166
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
167

G
ge-yafang 已提交
168
**返回值:**
P
PaDoBoo 已提交
169 170 171 172
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
173
**示例:**
W
wangxiyue 已提交
174
  ```ts
P
PaDaBoo 已提交
175
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
176
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
177
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
178
  
P
PaDaBoo 已提交
179 180 181 182 183 184 185
      let promise = data_preferences.deletePreferences(this.context, 'mystore')
      promise.then(() => {
          console.info("Deleted successfully.")
      }).catch((err) => {
          console.info("Deleted failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
186 187 188
  ```


P
PaDoBoo 已提交
189
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
190

191
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback<void>): void
P
PaDoBoo 已提交
192

G
ge-yafang 已提交
193 194 195
从内存中移除指定首选项持久化文件对应的Preferences单实例。

移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
196

P
PaDaBoo 已提交
197
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
198

G
ge-yafang 已提交
199
**参数:**
P
PaDoBoo 已提交
200 201
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
202
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
203
  | name | string | 是 | 应用程序内部数据存储名称。 |
204
  | callback | AsyncCallback<void> | 是 | 回调函数。 |
P
PaDoBoo 已提交
205

G
ge-yafang 已提交
206
**示例:**
W
wangxiyue 已提交
207
  ```ts
P
PaDaBoo 已提交
208
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
209
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
210
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
211
  
P
PaDaBoo 已提交
212 213 214 215 216 217 218 219
      data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) {
          if (err) {
              console.info("Removed preferences from cache failed, err: " + err)
              return
          }
          console.info("Removed preferences from cache successfully.")
      })
  }
P
PaDoBoo 已提交
220 221 222
  ```


P
PaDoBoo 已提交
223
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
224

P
PaDaBoo 已提交
225
removePreferencesFromCache(context: Context, name: string): Promise<void>
P
PaDoBoo 已提交
226

G
ge-yafang 已提交
227 228 229
从内存中移除指定首选项持久化文件对应的Preferences单实例。

移除Preferences单实例时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
230

P
PaDaBoo 已提交
231
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
232

G
ge-yafang 已提交
233
**参数:**
P
PaDoBoo 已提交
234 235
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
236
  | context | [Context](js-apis-Context.md) | 是 | 应用程序或功能的上下文。 |
P
PaDaBoo 已提交
237
  | name | string | 是 | 应用程序内部数据存储名称。 |
P
PaDoBoo 已提交
238

G
ge-yafang 已提交
239
**返回值:**
P
PaDoBoo 已提交
240 241 242 243
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
244
**示例:**
W
wangxiyue 已提交
245
  ```ts
P
PaDaBoo 已提交
246
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
247
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
248
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
249
  
P
PaDaBoo 已提交
250 251 252 253 254 255 256
      let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore')
      promise.then(() => {
          console.info("Removed preferences from cache successfully.")
      }).catch((err) => {
          console.info("Removed preferences from cache failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
257 258 259 260 261 262 263 264 265 266 267 268
  ```


## Preferences

提供获取和修改存储数据的接口。


### get

get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void

W
wangxiyue 已提交
269
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
270

P
PaDaBoo 已提交
271
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
272

G
ge-yafang 已提交
273
**参数:**
P
PaDoBoo 已提交
274 275
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
276
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
G
ge-yafang 已提交
277
  | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
278 279
  | callback | AsyncCallback<ValueType> | 是 | 回调函数。 |

G
ge-yafang 已提交
280
**示例:**
W
wangxiyue 已提交
281
  ```ts
P
PaDaBoo 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.get('startup', 'default', function(err, value) {
              if (err) {
                  console.info("Get the value of startup failed, err: " + err)
                  return
              }
              console.info("The value of startup is " + value)
          })
      })
  }
P
PaDoBoo 已提交
300 301 302 303 304 305 306
  ```


### get

get(key: string, defValue: ValueType): Promise<ValueType>

W
wangxiyue 已提交
307
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
308

P
PaDaBoo 已提交
309
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
310

P
PaDoBoo 已提交
311 312 313
- **参数:**
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
314
  | key | string | 是 | 要获取的存储key名称,不能为空。 |
G
ge-yafang 已提交
315
  | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
P
PaDoBoo 已提交
316

G
ge-yafang 已提交
317
**返回值:**
P
PaDoBoo 已提交
318 319 320 321
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<ValueType> | Promise实例,用于异步获取结果。 |

G
ge-yafang 已提交
322
**示例:**
W
wangxiyue 已提交
323
  ```ts
P
PaDaBoo 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promiseGet = preferences.get('startup', 'default')
          promiseGet.then((value) => {
              console.info("The value of startup is " + value)
          }).catch((err) => {
              console.info("Get the value of startup failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
340 341 342 343 344 345 346
  ```


### put

put(key: string, value: ValueType, callback: AsyncCallback<void>): void

W
wangxiyue 已提交
347
首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
348

P
PaDaBoo 已提交
349
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
350

G
ge-yafang 已提交
351
**参数:**
P
PaDoBoo 已提交
352 353
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
354
  | key | string | 是 | 要修改的存储的key,不能为空。 |
G
ge-yafang 已提交
355
  | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
356 357
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
358
**示例:**
W
wangxiyue 已提交
359
  ```ts
P
PaDaBoo 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.put('startup', 'auto', function (err) {
              if (err) {
                  console.info("Put the value of startup failed, err: " + err)
                  return
              }
              console.info("Put the value of startup successfully.")
          })
      })
  }
P
PaDoBoo 已提交
378 379 380 381 382 383 384
  ```


### put

put(key: string, value: ValueType): Promise<void>

W
wangxiyue 已提交
385
首先获取指定首选项持久化文件对应的Preferences实例,然后借助Preferences API将数据写入Preferences实例,通过flush或者flushSync将Preferences实例持久化,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
386

P
PaDaBoo 已提交
387
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
388

G
ge-yafang 已提交
389
**参数:**
P
PaDoBoo 已提交
390 391
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
392
  | key | string | 是 | 要修改的存储的key,不能为空。 |
G
ge-yafang 已提交
393
  | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
P
PaDoBoo 已提交
394

G
ge-yafang 已提交
395
**返回值:**
P
PaDoBoo 已提交
396 397 398 399
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
400
**示例:**
W
wangxiyue 已提交
401
  ```ts
P
PaDaBoo 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promisePut = preferences.put('startup', 'auto')
          promisePut.then(() => {
              console.info("Put the value of startup successfully.")
          }).catch((err) => {
              console.info("Put the value of startup failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
418 419 420 421 422 423 424
  ```


### has

has(key: string, callback: AsyncCallback<boolean>): boolean

W
wangxiyue 已提交
425
检查存储对象是否包含名为给定key的存储键值对,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
426

P
PaDaBoo 已提交
427
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
428

G
ge-yafang 已提交
429
**参数:**
P
PaDoBoo 已提交
430 431
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
432
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
433 434
  | callback | AsyncCallback<boolean> | 是 | 回调函数。 |

G
ge-yafang 已提交
435
**返回值:**
P
PaDoBoo 已提交
436 437 438 439
  | 类型 | 说明 |
  | -------- | -------- |
  | boolean | true表示存在,false表示不存在。 |

G
ge-yafang 已提交
440
**示例:**
W
wangxiyue 已提交
441
  ```ts
P
PaDaBoo 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.has('startup', function (err, isExist) {
              if (err) {
                  console.info("Check the key of startup failed, err: " + err)
                  return
              }
              if (isExist) {
                  console.info("The key of startup is contained.")
              } else {
                  console.info("The key of startup dose not contain.")
              }
          })
      })
  }
P
PaDoBoo 已提交
464 465 466 467 468 469 470
  ```


### has

has(key: string): Promise<boolean>

W
wangxiyue 已提交
471
检查存储对象是否包含名为给定key的存储键值对,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
472

P
PaDaBoo 已提交
473
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
474

G
ge-yafang 已提交
475
**参数:**
P
PaDoBoo 已提交
476 477
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
478
  | key | string | 是 | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
479

G
ge-yafang 已提交
480
**返回值:**
P
PaDoBoo 已提交
481 482 483 484
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<boolean> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
485
**示例:**
W
wangxiyue 已提交
486
  ```ts
P
PaDaBoo 已提交
487 488 489
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
G
ge-yafang 已提交
490
  
P
PaDaBoo 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promiseHas = preferences.has('startup')
          promiseHas.then((isExist) => {
              if (isExist) {
                  console.info("The key of startup is contained.")
              } else {
                  console.info("The key of startup dose not contain.")
              }
          }).catch((err) => {
              console.info("Check the key of startup failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
507 508 509 510 511 512 513
  ```


### delete

delete(key: string, callback: AsyncCallback<void>): void

W
wangxiyue 已提交
514
从存储对象中删除名为给定key的存储键值对,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
515

P
PaDaBoo 已提交
516
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
517

G
ge-yafang 已提交
518
**参数:**
P
PaDoBoo 已提交
519 520
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
521
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
522 523
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
524
**示例:**
W
wangxiyue 已提交
525
  ```ts
P
PaDaBoo 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.delete('startup', function (err) {
              if (err) {
                  console.info("Delete startup key failed, err: " + err)
                  return
              }
              console.info("Deleted startup key successfully.")
          })
      })
  }
P
PaDoBoo 已提交
544 545 546 547 548 549 550
  ```


### delete

delete(key: string): Promise<void>

W
wangxiyue 已提交
551
从存储对象删除名为给定key的存储键值对,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
552

P
PaDaBoo 已提交
553
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
554

G
ge-yafang 已提交
555
**参数:**
P
PaDoBoo 已提交
556 557
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
W
wangxiyue 已提交
558
  | key | string | 是 | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
559

G
ge-yafang 已提交
560
**返回值:**
P
PaDoBoo 已提交
561 562 563 564
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
565
**示例:**
W
wangxiyue 已提交
566
  ```ts
P
PaDaBoo 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promiseDelete = preferences.delete('startup')
          promiseDelete.then(() => {
              console.info("Deleted startup key successfully.")
          }).catch((err) => {
              console.info("Delete startup key failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
583 584 585 586 587 588 589
  ```


### flush

flush(callback: AsyncCallback<void>): void

W
wangxiyue 已提交
590
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
591

P
PaDaBoo 已提交
592
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
593

G
ge-yafang 已提交
594
**参数:**
P
PaDoBoo 已提交
595 596 597 598
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
599
**示例:**
W
wangxiyue 已提交
600
  ```ts
P
PaDaBoo 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.flush(function (err) {
              if (err) {
                  console.info("Flush to file failed, err: " + err)
                  return
              }
              console.info("Flushed to file successfully.")
          })
      })
  }
P
PaDoBoo 已提交
619 620 621 622 623 624 625
  ```


### flush

flush(): Promise<void>

W
wangxiyue 已提交
626
将当前preferences对象中的修改保存到当前的preferences,并异步存储到首选项持久化文件中,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
627

P
PaDaBoo 已提交
628
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
629

G
ge-yafang 已提交
630
**返回值:**
P
PaDoBoo 已提交
631 632 633 634
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
635
**示例:**
W
wangxiyue 已提交
636
  ```ts
P
PaDaBoo 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promiseFlush = preferences.flush()
          promiseFlush.then(() => {
              console.info("Flushed to file successfully.")
          }).catch((err) => {
              console.info("Flush to file failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
653 654 655 656 657 658 659
  ```


### clear

clear(callback: AsyncCallback<void>): void

W
wangxiyue 已提交
660
清除此存储对象中的所有存储,该方法使用callback方式作为异步方法。
P
PaDoBoo 已提交
661

P
PaDaBoo 已提交
662
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
663

G
ge-yafang 已提交
664
**参数:**
P
PaDoBoo 已提交
665 666 667 668
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

G
ge-yafang 已提交
669
**示例:**
W
wangxiyue 已提交
670
  ```ts
P
PaDaBoo 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.clear(function (err) {
              if (err) {
                  console.info("Clear to file failed, err: " + err)
                  return
              }
              console.info("Cleared to file successfully.")
          })
      })
  }
P
PaDoBoo 已提交
689 690 691 692 693 694 695
  ```


### clear

clear(): Promise<void>

W
wangxiyue 已提交
696
清除此存储对象中的所有存储,该方法使用Promise方式作为异步方法。
P
PaDoBoo 已提交
697

P
PaDaBoo 已提交
698
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
699

G
ge-yafang 已提交
700
**返回值:**
P
PaDoBoo 已提交
701 702 703 704
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

G
ge-yafang 已提交
705
**示例:**
W
wangxiyue 已提交
706
  ```ts
P
PaDaBoo 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          let promiseClear = preferences.clear()
          promiseClear.then(() => {
              console.info("Cleared to file successfully.")
          }).catch((err) => {
              console.info("Clear to file failed, err: " + err)
          })
      }).catch((err) => {
          console.info("Get the preferences failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
723 724 725 726 727 728 729 730 731
  ```


### on('change')

on(type: 'change', callback: Callback<{ key : string }>): void

订阅数据变更者类,订阅的key的值发生变更后,在执行flush方法后,callback方法会被回调。

P
PaDaBoo 已提交
732
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
733

G
ge-yafang 已提交
734
**参数:**
P
PaDoBoo 已提交
735 736 737 738 739
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 回调对象实例。 |

G
ge-yafang 已提交
740
**示例:**
W
wangxiyue 已提交
741
  ```ts
P
PaDaBoo 已提交
742 743 744 745 746 747
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
  
      var observer = function (key) {
          console.info("The key of " + key + " changed.")
P
PaDoBoo 已提交
748
      }
P
PaDaBoo 已提交
749
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
750
          if (err) {
P
PaDaBoo 已提交
751
              console.info("Get the preferences failed, err: " + err)
P
PaDoBoo 已提交
752 753
              return
          }
P
PaDaBoo 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
          preferences.on('change', observer)
          preferences.put('startup', 'auto', function (err) {
              if (err) {
                  console.info("Put the value of startup failed, err: " + err)
                  return
              }
              console.info("Put the value of startup successfully.")
              preferences.flush(function (err) {
                  if (err) {
                      console.info("Flush to file failed, err: " + err)
                      return
                  }
                  console.info("Flushed to file successfully.")    // observer will be called.
              })
          })  
P
PaDoBoo 已提交
769
      })
P
PaDaBoo 已提交
770
  }
P
PaDoBoo 已提交
771 772 773 774 775 776 777 778 779
  ```


### off('change')

off(type: 'change', callback: Callback<{ key : string }>): void

当不再进行订阅数据变更时,使用此接口取消订阅。

P
PaDaBoo 已提交
780
**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core
781

G
ge-yafang 已提交
782
**参数:**
P
PaDoBoo 已提交
783 784 785 786 787
  | 参数名 | 类型 | 说明 |
  | -------- | -------- | -------- |
  | type | string | 事件类型,固定值'change',表示数据变更。 |
  | callback | Callback<{ key : string }> | 需要取消的回调对象实例。 |

G
ge-yafang 已提交
788
**示例:**
W
wangxiyue 已提交
789
  ```ts
P
PaDaBoo 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
  import Ability from '@ohos.application.Ability'
  import data_preferences from '@ohos.data.preferences'
  export default class MainAbility extends Ability {
      var observer = function (key) {
          console.info("The key of " + key + " changed.")
      }
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
          if (err) {
              console.info("Get the preferences failed, err: " + err)
              return
          }
          preferences.on('change', observer)
          preferences.put('startup', 'auto', function (err) {
              if (err) {
                  console.info("Put the value of startup failed, err: " + err)
                  return
              }
              console.info("Put the value of startup successfully.")
              preferences.flush(function (err) {
                  if (err) {
                      console.info("Flush to file failed, err: " + err)
                      return
                  }
                  console.info("Flushed to file successfully.")    // observer will be called.
                  preferences.off('change', observer)
              })
          })  
      })
P
PaDoBoo 已提交
818 819
  }
  ```
G
ge-yafang 已提交
820 821 822 823 824 825 826 827 828 829 830 831

## ValueType

用于表示允许的数据字段类型。

**系统能力**:SystemCapability.DistributedDataManager.Preferences.Core

| 名称    | 说明                 |
| ------- | -------------------- |
| number  | 表示值类型为数字。   |
| string  | 表示值类型为字符。   |
| boolean | 表示值类型为布尔值。 |