js-apis-system-parameterEnhance.md 7.0 KB
Newer Older
H
handyohos 已提交
1
# @ohos.systemParameterEnhance (系统参数)
C
chengjinsong2 已提交
2 3

系统参数(SystemParameter)是为各系统服务提供的简单易用的键值对访问接口,各个系统服务可以定义系统参数来描述该服务的状态信息,或者通过系统参数来改变系统服务的行为。其基本操作原语为get和set,通过get可以查询系统参数的值,通过set可以修改系统参数的值。
Z
zengyawen 已提交
4
详细的系统参数设计原理及定义可参考[系统参数](../../../device-dev/subsystems/subsys-boot-init-sysparam.md)
5

6 7
> **说明:**
>
C
chengjinsong2 已提交
8
> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9
> - 本模块接口为系统接口。
C
chengjinsong2 已提交
10 11 12 13 14
> - 由于系统参数都是各个系统服务的内部信息和控制参数,每个系统参数都有各自不同的DAC和MAC访问控制权限,三方应用不能使用此类接口。

## 导入模块

```ts
C
fix ark  
chenshixu1 已提交
15
import systemparameter from '@ohos.systemParameterEnhance';
C
chengjinsong2 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
```

## systemparameter.getSync

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

获取系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待查询的系统参数Key。 |
C
chengjinsong2 已提交
31
| def | string | 否 | def为所要获取的系统参数的默认值 <br> def为可选参数,仅当系统参数不存在时生效 <br> def可以传undefined或自定义的任意值 |
C
chengjinsong2 已提交
32 33 34 35 36

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
C
chengjinsong2 已提交
37
| string | 系统参数值 <br> 若key存在,返回设定的值。 <br> 若key不存在且def有效,返回def;若未指定def或def无效(如undefined),抛异常。 |
C
chengjinsong2 已提交
38 39 40 41 42

**示例:**

```ts
try {
C
fix ark  
chenshixu1 已提交
43
    let info = systemparameter.getSync("const.ohos.apiversion");
C
chengjinsong2 已提交
44
    console.log(JSON.stringify(info));
C
chengjinsong2 已提交
45
} catch(e) {
C
chengjinsong2 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    console.log("getSync unexpected error: " + e);
}
```

## systemparameter.get

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

获取系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待查询的系统参数Key。 |
| callback | AsyncCallback&lt;string&gt; | 是 | 回调函数。 |

**示例:**

```ts
Z
zengyawen 已提交
68 69
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
70
try {
C
chenshixu1 已提交
71
    systemparameter.get("const.ohos.apiversion", (err: BusinessError, data: string) => {
C
chengjinsong2 已提交
72 73 74 75 76
    if (err == undefined) {
        console.log("get test.parameter.key value success:" + data)
    } else {
        console.log(" get test.parameter.key value err:" + err.code)
    }});
C
chengjinsong2 已提交
77
} catch(e) {
C
chengjinsong2 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    console.log("get unexpected error: " + e);
}
```

## systemparameter.get

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

获取系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待查询的系统参数Key。 |
| def | string | 是 | 默认值。 |
| callback | AsyncCallback&lt;string&gt; | 是 | 回调函数。 |

**示例:**

```ts
Z
zengyawen 已提交
101 102
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
103
try {
C
chenshixu1 已提交
104
    systemparameter.get("const.ohos.apiversion", "default", (err: BusinessError, data: string) => {
C
chengjinsong2 已提交
105 106 107 108 109 110
        if (err == undefined) {
            console.log("get test.parameter.key value success:" + data)
        } else {
            console.log(" get test.parameter.key value err:" + err.code)
        }
    });
C
chengjinsong2 已提交
111
} catch(e) {
C
chengjinsong2 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    console.log("get unexpected error:" + e)
}
```

## systemparameter.get

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

获取系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待查询的系统参数Key。 |
C
chengjinsong2 已提交
129
| def | string | 否 | def为所要获取的系统参数的默认值 <br> def为可选参数,仅当系统参数不存在时生效 <br> def可以传undefined或自定义的任意值 |
C
chengjinsong2 已提交
130 131 132 133 134 135 136 137 138 139

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;string&gt; | Promise示例,用于异步获取结果。 |

**示例:**

```ts
Z
zengyawen 已提交
140 141
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
142
try {
C
fix ark  
chenshixu1 已提交
143
    let p = systemparameter.get("const.ohos.apiversion");
C
chenshixu1 已提交
144
    p.then((value: string) => {
C
chengjinsong2 已提交
145
        console.log("get test.parameter.key success: " + value);
C
chenshixu1 已提交
146
    }).catch((err: BusinessError) => {
C
chengjinsong2 已提交
147 148
        console.log("get test.parameter.key error: " + err.code);
    });
C
chengjinsong2 已提交
149
} catch(e) {
C
chengjinsong2 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    console.log("get unexpected error: " + e);
}
```

## systemparameter.setSync

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

设置系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待设置的系统参数Key。 |
| value | string | 是 | 待设置的系统参数值。 |

**示例:**

```ts
Z
zengyawen 已提交
172 173
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
174 175
try {
    systemparameter.setSync("test.parameter.key", "default");
C
chengjinsong2 已提交
176
} catch(e) {
C
chengjinsong2 已提交
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

设置系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待设置的系统参数Key。 |
| value | string | 是 | 待设置的系统参数值。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |

**示例:**

```ts
Z
zengyawen 已提交
200 201
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
202
try {
C
chenshixu1 已提交
203
    systemparameter.set("test.parameter.key", "testValue", (err: BusinessError, data: void) => {
C
chengjinsong2 已提交
204 205 206 207 208
    if (err == undefined) {
        console.log("set test.parameter.key value success :" + data)
    } else {
        console.log("set test.parameter.key value err:" + err.code)
    }});
C
chengjinsong2 已提交
209
} catch(e) {
C
chengjinsong2 已提交
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 236 237
    console.log("set unexpected error: " + e);
}
```

## systemparameter.set

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

设置系统参数Key对应的值。

**系统能力:** SystemCapability.Startup.SystemInfo

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 待设置的系统参数Key。 |
| value| string | 是 | 待设置的系统参数值。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | Promise示例,用于异步获取结果。 |

**示例:**

```ts
Z
zengyawen 已提交
238 239
import { BusinessError } from '@ohos.base';

C
chengjinsong2 已提交
240
try {
C
fix ark  
chenshixu1 已提交
241
    let p = systemparameter.set("test.parameter.key", "testValue");
C
chenshixu1 已提交
242
    p.then((value: void) => {
C
chengjinsong2 已提交
243
        console.log("set test.parameter.key success: " + value);
C
chenshixu1 已提交
244
    }).catch((err: BusinessError) => {
C
chengjinsong2 已提交
245 246
        console.log(" set test.parameter.key error: " + err.code);
    });
C
chengjinsong2 已提交
247
} catch(e) {
C
chengjinsong2 已提交
248 249 250
    console.log("set unexpected error: " + e);
}
```