js-apis-system-parameterEnhance.md 7.3 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.systemParameterEnhance (System Parameter)
E
ester.zhou 已提交
2 3 4 5 6 7

The **SystemParameter** module provides system services with easy access to key-value pairs. You can use the APIs provided by this module to describe the service status and change the service behavior. The basic operation primitives are get and set. You can obtain the values of system parameters through getters and modify the values through setters.
For details about the system parameter design principles and definitions, see
[Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md).

> **NOTE**
E
ester.zhou 已提交
8
>
E
ester.zhou 已提交
9 10
> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
> - The APIs provided by this module are system APIs.
E
esterzhou 已提交
11
> - Third-party applications cannot use the APIs provided by this module, because system parameters each require specific discretionary access control (DAC) and mandatory access control (MAC) permissions.
E
ester.zhou 已提交
12 13 14 15

## Modules to Import

```ts
E
ester.zhou 已提交
16
import systemparameter from '@ohos.systemParameterEnhance'
E
ester.zhou 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```

## systemparameter.getSync

getSync(key: string, def?: string): string

Obtains the value of the system parameter with the specified key.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
E
ester.zhou 已提交
32
| def | string | No| Default value of the system parameter.<br>It works only when the system parameter does not exist.<br>The value can be **undefined** or any custom value. |
E
ester.zhou 已提交
33 34 35 36 37

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
38
| string | Value of the system parameter.<br>If the specified key exists, the set value is returned.<br>If the specified key does not exist and **def** is set to a valid value, the set value is returned. If the specified key does not exist and **def** is set to an invalid value (such as **undefined**) or is not set, an exception is thrown. |
E
ester.zhou 已提交
39 40 41 42 43 44 45

**Example**

```ts
try {
    var info = systemparameter.getSync("const.ohos.apiversion");
    console.log(JSON.stringify(info));
E
ester.zhou 已提交
46
} catch(e) {
E
ester.zhou 已提交
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
    console.log("getSync unexpected error: " + e);
}
```

## systemparameter.get

get(key: string, callback: AsyncCallback&lt;string&gt;): void

Obtains the value of the system parameter with the specified key.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|

**Example**

```ts
try {
    systemparameter.get("const.ohos.apiversion", function (err, data) {
    if (err == undefined) {
        console.log("get test.parameter.key value success:" + data)
    } else {
        console.log(" get test.parameter.key value err:" + err.code)
    }});
E
ester.zhou 已提交
76
} catch(e) {
E
ester.zhou 已提交
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 106 107
    console.log("get unexpected error: " + e);
}
```

## systemparameter.get

get(key: string, def: string, callback: AsyncCallback&lt;string&gt;): void

Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
| def | string | Yes| Default value.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|

**Example**

```ts
try {
    systemparameter.get("const.ohos.apiversion", "default", function (err, data) {
        if (err == undefined) {
            console.log("get test.parameter.key value success:" + data)
        } else {
            console.log(" get test.parameter.key value err:" + err.code)
        }
    });
E
ester.zhou 已提交
108
} catch(e) {
E
ester.zhou 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    console.log("get unexpected error:" + e)
}
```

## systemparameter.get

get(key: string, def?: string): Promise&lt;string&gt;

Obtains the value of the system parameter with the specified key. This API uses a promise to return the result.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
E
ester.zhou 已提交
126
| def | string | No| Default value of the system parameter.<br>It works only when the system parameter does not exist.<br>The value can be **undefined** or any custom value. |
E
ester.zhou 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;string&gt; | Promise used to return the execution result.|

**Example**

```ts
try {
    var p = systemparameter.get("const.ohos.apiversion");
    p.then(function (value) {
        console.log("get test.parameter.key success: " + value);
    }).catch(function (err) {
        console.log("get test.parameter.key error: " + err.code);
    });
E
ester.zhou 已提交
144
} catch(e) {
E
ester.zhou 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    console.log("get unexpected error: " + e);
}
```

## systemparameter.setSync

setSync(key: string, value: string): void

Sets a value for the system parameter with the specified key.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
| value | string | Yes| Value of the system parameter to set.|

**Example**

```ts
try {
    systemparameter.setSync("test.parameter.key", "default");
E
ester.zhou 已提交
169
} catch(e) {
E
ester.zhou 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    console.log("set unexpected error: " + e);
}
```

## systemparameter.set

set(key: string, value: string, callback: AsyncCallback&lt;void&gt;): void

Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
| value | string | Yes| Value of the system parameter to set.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|

**Example**

```ts
try {
    systemparameter.set("test.parameter.key", "testValue", function (err, data) {
    if (err == undefined) {
        console.log("set test.parameter.key value success :" + data)
    } else {
        console.log("set test.parameter.key value err:" + err.code)
    }});
E
ester.zhou 已提交
200
} catch(e) {
E
ester.zhou 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    console.log("set unexpected error: " + e);
}
```

## systemparameter.set

set(key: string, value: string): Promise&lt;void&gt;

Sets a value for the system parameter with the specified key. This API uses a promise to return the result.

**System capability**: SystemCapability.Startup.SystemInfo

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system parameter.|
| value| string | Yes| Value of the system parameter to set.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the execution result.|

**Example**

```ts
try {
    var p = systemparameter.set("test.parameter.key", "testValue");
    p.then(function (value) {
        console.log("set test.parameter.key success: " + value);
    }).catch(function (err) {
        console.log(" set test.parameter.key error: " + err.code);
    });
E
ester.zhou 已提交
236
} catch(e) {
E
ester.zhou 已提交
237 238 239
    console.log("set unexpected error: " + e);
}
```