js-apis-settings.md 4.2 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2
# Settings

E
esterzhou 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
E
add doc  
ester.zhou 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17
> The initial APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.


This module provides APIs for setting data items.


## Modules to Import

```
import settings from '@ohos.settings';
```



L
lvxiaoqiang 已提交
18
## settings.getUriSync
E
add doc  
ester.zhou 已提交
19

L
lvxiaoqiang 已提交
20
getUriSync(name: string): string
E
add doc  
ester.zhou 已提交
21 22 23

Obtains the URI of a data item.

E
esterzhou 已提交
24 25
**System capability**: SystemCapability.Applictaions.settings.Core

E
add doc  
ester.zhou 已提交
26 27 28
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
29
  | name | string | Yes| Name of the target data item. Data items can be classified as follows:<br> <ul><li>Existing data items in the database, for example:<br></li> <ul><li>Brightness: 'settings.screen.brightness'<br> </li>  <li>Time format: 'settings.time.format'<br> </li></ul> <li>Custom data items</li></ul>|
E
add doc  
ester.zhou 已提交
30 31 32 33 34 35 36 37 38

- Return value
  | Type| Description|
  | -------- | -------- |
  | string | URI of the data item.|

- Example
  ```
   // Obtain the URI of a data item.
L
lvxiaoqiang 已提交
39
   let urivar = settings.getUriSync('settings.screen.brightness');  
E
add doc  
ester.zhou 已提交
40 41 42
  ```


L
lvxiaoqiang 已提交
43
## settings.getValueSync
E
add doc  
ester.zhou 已提交
44

L
lvxiaoqiang 已提交
45
getValueSync(dataAbilityHelper: DataAbilityHelper, name: string, defValue: string): string
E
add doc  
ester.zhou 已提交
46 47 48

Obtains the value of a data item.

E
esterzhou 已提交
49 50
**System capability**: SystemCapability.Applictaions.settings.Core

E
add doc  
ester.zhou 已提交
51 52 53 54
- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
E
esterzhou 已提交
55
  | name | string | Yes| Name of the target data item. Data items can be classified as follows:<br> <ul><li>Existing data items in the database, for example:<br></li> <ul><li>Brightness: 'settings.screen.brightness'<br> </li>  <li>Time format: 'settings.time.format'<br> </li></ul> <li>Custom data items</li></ul>|
E
add doc  
ester.zhou 已提交
56 57 58 59 60 61 62 63 64 65
  | defValue | string | Yes| Default value This parameter is user-defined. If it is not found in the database, the default value is returned.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | string | Value of the data item.|

- Example
  ```
  import featureAbility from '@ohos.featureAbility';
E
esterzhou 已提交
66

E
add doc  
ester.zhou 已提交
67 68
  // Obtain the value of 'settings.screen.brightness' (this data item already exists in the database).
  let brightness = 'settings.screen.brightness';
L
lvxiaoqiang 已提交
69
  let uri = settings.getUriSync(brightness);
E
add doc  
ester.zhou 已提交
70
  let helper = featureAbility.acquireDataAbilityHelper(uri);
L
lvxiaoqiang 已提交
71
  let value = settings.getValueSync(helper, brightness, '10');
E
add doc  
ester.zhou 已提交
72 73 74
  ```


L
lvxiaoqiang 已提交
75
## settings.setValueSync
E
add doc  
ester.zhou 已提交
76

L
lvxiaoqiang 已提交
77
setValueSync(dataAbilityHelper: DataAbilityHelper, name: string, value: string): boolean
E
add doc  
ester.zhou 已提交
78 79

Sets the value of a data item.
L
lvxiaoqiang 已提交
80
If the specified data item exists in the database, the **setValueSync** method updates the value of the data item. If the data item does not exist in the database, the **setValueSync** method inserts the data item into the database.
E
add doc  
ester.zhou 已提交
81

E
esterzhou 已提交
82 83 84
**Required permissions**: ohos.permission.WRITE_SYSTEM_SETTING

**System capability**: SystemCapability.Applictaions.settings.Core
E
add doc  
ester.zhou 已提交
85 86 87 88 89

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
E
esterzhou 已提交
90
  | name | string | Yes| Name of the target data item. Data items can be classified as follows:<br> <ul><li>Existing data items in the database, for example:<br></li> <ul><li>Brightness: 'settings.screen.brightness'<br> </li>  <li>Time format: 'settings.time.format'<br> </li></ul> <li>Custom data items</li></ul>|
E
add doc  
ester.zhou 已提交
91 92 93 94 95 96 97 98 99 100
  | value | string | Yes| Value of the data item.|

- Return value
  | Type| Description|
  | -------- | -------- |
  | boolean | Result indicating whether the value is set successfully. Returns **true** if the value is set successfully; returns **false** otherwise.|

- Example
  ```
  import featureAbility from '@ohos.featureAbility';
E
esterzhou 已提交
101

L
lvxiaoqiang 已提交
102
  // Update the value of 'settings.screen.brightness'. (As this data item exists in the database, the setValueSync method
E
ester.zhou 已提交
103
     will update the value of the data item.)
E
add doc  
ester.zhou 已提交
104
  let brightness = 'settings.screen.brightness';
L
lvxiaoqiang 已提交
105
  let uri = settings.getUriSync(brightness);
E
add doc  
ester.zhou 已提交
106
  let helper = featureAbility.acquireDataAbilityHelper(uri);
L
lvxiaoqiang 已提交
107
  let ret = settings.setValueSync(helper, brightness, '100');
E
add doc  
ester.zhou 已提交
108
  ```