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

E
ester.zhou 已提交
3
The **Settings** module provides APIs for setting data items.
C
chenqi 已提交
4

E
ester.zhou 已提交
5 6
> **NOTE**
>
E
add doc  
ester.zhou 已提交
7 8 9 10 11
> 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.


## Modules to Import

C
chenqi 已提交
12
```ts
E
add doc  
ester.zhou 已提交
13 14 15 16 17
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.

G
ge-yafang 已提交
24
**System capability**: SystemCapability.Applications.settings.Core
E
esterzhou 已提交
25

E
ester.zhou 已提交
26
**Parameters**
E
add doc  
ester.zhou 已提交
27

E
ester.zhou 已提交
28 29
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
C
chenqi 已提交
30 31
| settings.display.SCREEN_BRIGHTNESS_STATUS | string | Yes| Brightness of the target data item.|
| settings.date.TIME_FORMAT | string | Yes| Time format of the target data item. Data |
E
add doc  
ester.zhou 已提交
32

E
ester.zhou 已提交
33 34 35 36 37 38 39 40
**Return value**

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

**Example**

C
chenqi 已提交
41
```ts
E
ester.zhou 已提交
42
 // Obtain the URI of a data item.
L
lvxiaoqiang 已提交
43
 let urivar = settings.getUriSync(settings.display.SCREEN_BRIGHTNESS_STATUS);  
E
ester.zhou 已提交
44
```
E
add doc  
ester.zhou 已提交
45 46


L
lvxiaoqiang 已提交
47
## settings.getValueSync
E
add doc  
ester.zhou 已提交
48

L
lvxiaoqiang 已提交
49
getValueSync(dataAbilityHelper: DataAbilityHelper, name: string, defValue: string): string
E
add doc  
ester.zhou 已提交
50 51 52

Obtains the value of a data item.

G
ge-yafang 已提交
53
**System capability**: SystemCapability.Applications.settings.Core
E
esterzhou 已提交
54

E
ester.zhou 已提交
55 56 57 58
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
E
ester.zhou 已提交
59
| 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.display.SCREEN_BRIGHTNESS_STATUS**<br> </li>  <li>Time format: **settings.date.TIME_FORMAT**<br> </li></ul> <li>Custom data items</li></ul> |
E
ester.zhou 已提交
60 61 62 63 64 65 66
| 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.|
E
add doc  
ester.zhou 已提交
67

E
ester.zhou 已提交
68
**Example**
E
add doc  
ester.zhou 已提交
69

C
chenqi 已提交
70
```ts
L
lvxiaoqiang 已提交
71
  import featureAbility from '@ohos.ability.featureAbility';
E
esterzhou 已提交
72

L
lvxiaoqiang 已提交
73 74
// Obtain the value of settings.display.SCREEN_BRIGHTNESS_STATUS (this data item already exists in the database).
let uri = settings.getUriSync(settings.display.SCREEN_BRIGHTNESS_STATUS);
E
ester.zhou 已提交
75
let helper = featureAbility.acquireDataAbilityHelper(uri);
L
lvxiaoqiang 已提交
76
let value = settings.getValueSync(helper, settings.display.SCREEN_BRIGHTNESS_STATUS, '10');
E
ester.zhou 已提交
77
```
E
add doc  
ester.zhou 已提交
78 79


L
lvxiaoqiang 已提交
80
## settings.setValueSync
E
add doc  
ester.zhou 已提交
81

L
lvxiaoqiang 已提交
82
setValueSync(dataAbilityHelper: DataAbilityHelper, name: string, value: string): boolean
E
add doc  
ester.zhou 已提交
83 84

Sets the value of a data item.
E
ester.zhou 已提交
85

L
lvxiaoqiang 已提交
86
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 已提交
87

L
lvxiaoqiang 已提交
88
**Required permissions**: ohos.permission.MODIFY_SETTINGS
E
esterzhou 已提交
89

G
ge-yafang 已提交
90
**System capability**: SystemCapability.Applications.settings.Core
E
add doc  
ester.zhou 已提交
91

E
ester.zhou 已提交
92 93 94 95 96
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
E
ester.zhou 已提交
97
| 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.display.SCREEN_BRIGHTNESS_STATUS**<br> </li>  <li>Time format: **settings.date.TIME_FORMAT**<br> </li></ul> <li>Custom data items</li></ul> |
C
chenqi 已提交
98
| value | string | Yes| Value of the data item.The value of range with the service.|
E
add doc  
ester.zhou 已提交
99

E
ester.zhou 已提交
100
**Return value**
E
add doc  
ester.zhou 已提交
101

E
ester.zhou 已提交
102 103 104 105 106 107
| Type| Description|
| -------- | -------- |
| boolean | Result indicating whether the value is set successfully. Returns **true** if the value is set successfully; returns **false** otherwise.|

**Example**

C
chenqi 已提交
108
```ts
L
lvxiaoqiang 已提交
109
  import featureAbility from '@ohos.ability.featureAbility';
E
esterzhou 已提交
110

L
lvxiaoqiang 已提交
111
// Update the value of settings.display.SCREEN_BRIGHTNESS_STATUS. (As this data item exists in the database, the setValueSync 
E
ester.zhou 已提交
112
   method will update the value of the data item.)
L
lvxiaoqiang 已提交
113
let uri = settings.getUriSync(settings.display.SCREEN_BRIGHTNESS_STATUS);
E
ester.zhou 已提交
114
let helper = featureAbility.acquireDataAbilityHelper(uri);
L
lvxiaoqiang 已提交
115
let ret = settings.setValueSync(helper, settings.display.SCREEN_BRIGHTNESS_STATUS, '100');
E
ester.zhou 已提交
116
```