js-apis-settings.md 4.0 KB
Newer Older
E
add doc  
ester.zhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
# Settings

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> 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';
```


## Required Permissions

None


## settings.getUri

getUri(name: string): string

Obtains the URI of a data item.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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>|

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

- Example
  ```
   // Obtain the URI of a data item.
   let urivar = settings.getUri('settings.screen.brightness');  
  ```


## settings.getValue

getValue(dataAbilityHelper: DataAbilityHelper, name: string, defValue: string): string

Obtains the value of a data item.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
  | 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>|
  | 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';
  
  // Obtain the value of 'settings.screen.brightness' (this data item already exists in the database).
  let brightness = 'settings.screen.brightness';
  let uri = settings.getUri(brightness);
  let helper = featureAbility.acquireDataAbilityHelper(uri);
  let value = settings.getValue(helper, brightness, '10');
  ```


## settings.setValue

setValue(dataAbilityHelper: DataAbilityHelper, name: string, value: string): boolean

Sets the value of a data item.
If the specified data item exists in the database, the **setValue** method updates the value of the data item. If the data item does not exist in the database, the **setValue** method inserts the data item into the database.

To use this method, you must have the **ohos.permission.WRITE_SYSTEM_SETTING** permission.

- Parameters
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | dataAbilityHelper | [DataAbilityHelper](js-apis-dataAbilityHelper.md) | Yes| **DataAbilityHelper** class.|
  | 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>|
  | 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';
  
  // Update the value of 'settings.screen.brightness'. (As this data item exists in the database, the setValue method will update the value of the data item.)
  let brightness = 'settings.screen.brightness';
  let uri = settings.getUri(brightness);
  let helper = featureAbility.acquireDataAbilityHelper(uri);
  let ret = settings.setValue(helper, brightness, '100');
  ```