js-apis-data-preferences.md 28.0 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
P
PaDoBoo 已提交
13
import data_preferences from '@ohos.data.preferences'
P
PaDoBoo 已提交
14 15 16 17
```

## 属性

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

P
PaDoBoo 已提交
20 21
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
22 23
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,大小为80字节。 |
| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 |
W
wangxiyue 已提交
24
| ValueType | number丨string丨boolean | 是 | 否 | 默认返回值,支持number、string、boolean。 |
P
PaDoBoo 已提交
25 26


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

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

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

P
PaDaBoo 已提交
33

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

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

- 示例:
W
wangxiyue 已提交
44
  ```ts
P
PaDoBoo 已提交
45
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
46
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
47
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
48

P
PaDaBoo 已提交
49
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
50
          if (err) {
P
PaDaBoo 已提交
51 52
              console.info("Get the preferences failed")
              return;
P
PaDoBoo 已提交
53
          }
P
PaDaBoo 已提交
54
          preferences.put('startup', 'auto', function (err) {
P
PaDoBoo 已提交
55
              if (err) {
P
PaDaBoo 已提交
56
                  console.info("Put the value of startup failed, err: " + err)
P
PaDoBoo 已提交
57 58
                  return
              }
P
PaDaBoo 已提交
59 60 61 62 63 64 65 66
              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 已提交
67 68
          })
      })
P
PaDaBoo 已提交
69
  }
P
PaDoBoo 已提交
70 71 72
  ```


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

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<[Preferences](#preferences)> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
93
  ```ts
P
PaDaBoo 已提交
94
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
95
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
96
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
97

P
PaDaBoo 已提交
98 99 100
      let promise = data_preferences.getPreferences(this.context, 'mystore')
      promise.then((preferences) => {
          preferences.put('startup', 'auto', function (err) {
P
PaDoBoo 已提交
101
              if (err) {
P
PaDaBoo 已提交
102
                  console.info("Put the value of startup failed, err: " + err)
P
PaDoBoo 已提交
103 104
                  return
              }
P
PaDaBoo 已提交
105 106 107 108 109 110 111 112
              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 已提交
113
          })
P
PaDaBoo 已提交
114 115
      }).catch((err) => {
          console.info("Get the preferences failed")
P
PaDoBoo 已提交
116
      })
P
PaDaBoo 已提交
117
  }
P
PaDoBoo 已提交
118 119 120
  ```


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

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

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

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

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

- 示例:
W
wangxiyue 已提交
138
  ```ts
P
PaDaBoo 已提交
139
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
140
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
141
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
142

P
PaDaBoo 已提交
143 144 145 146 147 148 149 150
      data_preferences.deletePreferences(this.context, 'mystore', function (err) {
          if (err) {
              console.info("Deleted failed, err: " + err)
              return
          }
          console.info("Deleted successfully.")
      })
  }
P
PaDoBoo 已提交
151 152 153
  ```


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

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
175
  ```ts
P
PaDaBoo 已提交
176
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
177
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
178
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
179

P
PaDaBoo 已提交
180 181 182 183 184 185 186
      let promise = data_preferences.deletePreferences(this.context, 'mystore')
      promise.then(() => {
          console.info("Deleted successfully.")
      }).catch((err) => {
          console.info("Deleted failed, err: " + err)
      })
  }
P
PaDoBoo 已提交
187 188 189
  ```


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

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

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

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

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

- 示例:
W
wangxiyue 已提交
206
  ```ts
P
PaDaBoo 已提交
207
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
208
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
209
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
210

P
PaDaBoo 已提交
211 212 213 214 215 216 217 218
      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 已提交
219 220 221
  ```


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

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
242
  ```ts
P
PaDaBoo 已提交
243
  import Ability from '@ohos.application.Ability'
P
PaDoBoo 已提交
244
  import data_preferences from '@ohos.data.preferences'
P
PaDaBoo 已提交
245
  export default class MainAbility extends Ability {
W
wangxiyue 已提交
246

P
PaDaBoo 已提交
247 248 249 250 251 252 253
      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 已提交
254 255 256 257 258 259 260 261 262 263 264 265
  ```


## Preferences

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


### get

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

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

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

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

- 示例:
W
wangxiyue 已提交
278
  ```ts
P
PaDaBoo 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  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 已提交
297 298 299 300 301 302 303
  ```


### get

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<ValueType> | Promise实例,用于异步获取结果。 |

- 示例:
W
wangxiyue 已提交
320
  ```ts
P
PaDaBoo 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  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 已提交
337 338 339 340 341 342 343
  ```


### put

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

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

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

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

- 示例:
W
wangxiyue 已提交
356
  ```ts
P
PaDaBoo 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
  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 已提交
375 376 377 378 379 380 381
  ```


### put

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
398
  ```ts
P
PaDaBoo 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  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 已提交
415 416 417 418 419 420 421
  ```


### has

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

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | boolean | true表示存在,false表示不存在。 |

- 示例:
W
wangxiyue 已提交
438
  ```ts
P
PaDaBoo 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  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 已提交
461 462 463 464 465 466 467
  ```


### has

has(key: string): Promise<boolean>

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<boolean> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
483
  ```ts
P
PaDaBoo 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  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 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 已提交
504 505 506 507 508 509 510
  ```


### delete

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

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

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

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

- 示例:
W
wangxiyue 已提交
522
  ```ts
P
PaDaBoo 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
  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 已提交
541 542 543 544 545 546 547
  ```


### delete

delete(key: string): Promise<void>

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

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

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

- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
563
  ```ts
P
PaDaBoo 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  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 已提交
580 581 582 583 584 585 586
  ```


### flush

flush(callback: AsyncCallback<void>): void

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

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

P
PaDoBoo 已提交
591 592 593 594 595 596
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
597
  ```ts
P
PaDaBoo 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
  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 已提交
616 617 618 619 620 621 622
  ```


### flush

flush(): Promise<void>

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

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

P
PaDoBoo 已提交
627 628 629 630 631 632
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
633
  ```ts
P
PaDaBoo 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  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 已提交
650 651 652 653 654 655 656
  ```


### clear

clear(callback: AsyncCallback<void>): void

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

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

P
PaDoBoo 已提交
661 662 663 664 665 666
- 参数:
  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback<void> | 是 | 回调函数。 |

- 示例:
W
wangxiyue 已提交
667
  ```ts
P
PaDaBoo 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
  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 已提交
686 687 688 689 690 691 692
  ```


### clear

clear(): Promise<void>

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

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

P
PaDoBoo 已提交
697 698 699 700 701 702
- 返回值:
  | 类型 | 说明 |
  | -------- | -------- |
  | Promise<void> | Promise实例,用于异步处理。 |

- 示例:
W
wangxiyue 已提交
703
  ```ts
P
PaDaBoo 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  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 已提交
720 721 722 723 724 725 726 727 728
  ```


### on('change')

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

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

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

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

- 示例:
W
wangxiyue 已提交
738
  ```ts
P
PaDaBoo 已提交
739 740 741 742 743 744
  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 已提交
745
      }
P
PaDaBoo 已提交
746
      data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
P
PaDoBoo 已提交
747
          if (err) {
P
PaDaBoo 已提交
748
              console.info("Get the preferences failed, err: " + err)
P
PaDoBoo 已提交
749 750
              return
          }
P
PaDaBoo 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
          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 已提交
766
      })
P
PaDaBoo 已提交
767
  }
P
PaDoBoo 已提交
768 769 770 771 772 773 774 775 776
  ```


### off('change')

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

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

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

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

- 示例:
W
wangxiyue 已提交
786
  ```ts
P
PaDaBoo 已提交
787 788 789 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
  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 已提交
815 816
  }
  ```