1# @ohos.systemParameterEnhance (系统参数) 2 3系统参数(SystemParameter)是为各系统服务提供的简单易用的键值对访问接口,各个系统服务可以定义系统参数来描述该服务的状态信息,或者通过系统参数来改变系统服务的行为。其基本操作原语为get和set,通过get可以查询系统参数的值,通过set可以修改系统参数的值。 4详细的系统参数设计原理及定义可参考 5[系统参数](../../../device-dev/subsystems/subsys-boot-init-sysparam.md)。 6 7> **说明:** 8> 9> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10> - 本模块接口为系统接口。 11> - 由于系统参数都是各个系统服务的内部信息和控制参数,每个系统参数都有各自不同的DAC和MAC访问控制权限,三方应用不能使用此类接口。 12 13## 导入模块 14 15```ts 16import systemparameter from '@ohos.systemParameterEnhance'; 17``` 18 19## systemparameter.getSync 20 21getSync(key: string, def?: string): string 22 23获取系统参数Key对应的值。 24 25**系统能力:** SystemCapability.Startup.SystemInfo 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| -------- | -------- | -------- | -------- | 31| key | string | 是 | 待查询的系统参数Key。 | 32| def | string | 否 | def为所要获取的系统参数的默认值 <br> def为可选参数,仅当系统参数不存在时生效 <br> def可以传undefined或自定义的任意值 | 33 34**返回值:** 35 36| 类型 | 说明 | 37| -------- | -------- | 38| string | 系统参数值 <br> 若key存在,返回设定的值。 <br> 若key不存在且def有效,返回def;若未指定def或def无效(如undefined),抛异常。 | 39 40**错误码**: 41 42| 错误码ID | 错误信息 | 43| -------- | ------------------------------------------------------------ | 44| 14700101 | if key is not found | 45| 14700103 | if permission denied | 46| 14700104 | if system internal error | 47 48以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 49 50**示例:** 51 52```ts 53try { 54 let info: string = systemparameter.getSync("const.ohos.apiversion"); 55 console.log(JSON.stringify(info)); 56} catch(e) { 57 console.log("getSync unexpected error: " + e); 58} 59``` 60 61## systemparameter.get 62 63get(key: string, callback: AsyncCallback<string>): void 64 65获取系统参数Key对应的值。 66 67**系统能力:** SystemCapability.Startup.SystemInfo 68 69**参数:** 70 71| 参数名 | 类型 | 必填 | 说明 | 72| -------- | -------- | -------- | -------- | 73| key | string | 是 | 待查询的系统参数Key。 | 74| callback | AsyncCallback<string> | 是 | 回调函数。 | 75 76**错误码**: 77 78| 错误码ID | 错误信息 | 79| -------- | ------------------------------------------------------------ | 80| 14700101 | if key is not found | 81| 14700103 | if permission denied | 82| 14700104 | if system internal error | 83 84以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 85 86**示例:** 87 88```ts 89import { BusinessError } from '@ohos.base'; 90 91try { 92 systemparameter.get("const.ohos.apiversion", (err: BusinessError, data: string) => { 93 if (err == undefined) { 94 console.log("get test.parameter.key value success:" + data) 95 } else { 96 console.log(" get test.parameter.key value err:" + err.code) 97 }}); 98} catch(e) { 99 console.log("get unexpected error: " + e); 100} 101``` 102 103## systemparameter.get 104 105get(key: string, def: string, callback: AsyncCallback<string>): void 106 107获取系统参数Key对应的值。 108 109**系统能力:** SystemCapability.Startup.SystemInfo 110 111**参数:** 112 113| 参数名 | 类型 | 必填 | 说明 | 114| -------- | -------- | -------- | -------- | 115| key | string | 是 | 待查询的系统参数Key。 | 116| def | string | 是 | 默认值。 | 117| callback | AsyncCallback<string> | 是 | 回调函数。 | 118 119**错误码**: 120 121| 错误码ID | 错误信息 | 122| -------- | ------------------------------------------------------------ | 123| 14700101 | if key is not found | 124| 14700103 | if permission denied | 125| 14700104 | if system internal error | 126 127以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 128 129**示例:** 130 131```ts 132import { BusinessError } from '@ohos.base'; 133 134try { 135 systemparameter.get("const.ohos.apiversion", "default", (err: BusinessError, data: string) => { 136 if (err == undefined) { 137 console.log("get test.parameter.key value success:" + data) 138 } else { 139 console.log(" get test.parameter.key value err:" + err.code) 140 } 141 }); 142} catch(e) { 143 console.log("get unexpected error:" + e) 144} 145``` 146 147## systemparameter.get 148 149get(key: string, def?: string): Promise<string> 150 151获取系统参数Key对应的值。 152 153**系统能力:** SystemCapability.Startup.SystemInfo 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| -------- | -------- | -------- | -------- | 159| key | string | 是 | 待查询的系统参数Key。 | 160| def | string | 否 | def为所要获取的系统参数的默认值 <br> def为可选参数,仅当系统参数不存在时生效 <br> def可以传undefined或自定义的任意值 | 161 162**返回值:** 163 164| 类型 | 说明 | 165| -------- | -------- | 166| Promise<string> | Promise示例,用于异步获取结果。 | 167 168**错误码**: 169 170| 错误码ID | 错误信息 | 171| -------- | ------------------------------------------------------------ | 172| 14700101 | if key is not found | 173| 14700103 | if permission denied | 174| 14700104 | if system internal error | 175 176以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 177 178**示例:** 179 180```ts 181import { BusinessError } from '@ohos.base'; 182 183try { 184 let p: Promise<string> = systemparameter.get("const.ohos.apiversion"); 185 p.then((value: string) => { 186 console.log("get test.parameter.key success: " + value); 187 }).catch((err: BusinessError) => { 188 console.log("get test.parameter.key error: " + err.code); 189 }); 190} catch(e) { 191 console.log("get unexpected error: " + e); 192} 193``` 194 195## systemparameter.setSync 196 197setSync(key: string, value: string): void 198 199设置系统参数Key对应的值。 200 201**系统能力:** SystemCapability.Startup.SystemInfo 202 203**参数:** 204 205| 参数名 | 类型 | 必填 | 说明 | 206| -------- | -------- | -------- | -------- | 207| key | string | 是 | 待设置的系统参数Key。 | 208| value | string | 是 | 待设置的系统参数值。 | 209 210**错误码**: 211 212| 错误码ID | 错误信息 | 213| -------- | ------------------------------------------------------------ | 214| 14700102 | if value is invalid | 215| 14700103 | if permission denied | 216| 14700104 | if system internal error | 217 218以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 219 220**示例:** 221 222```ts 223import { BusinessError } from '@ohos.base'; 224 225try { 226 systemparameter.setSync("test.parameter.key", "default"); 227} catch(e) { 228 console.log("set unexpected error: " + e); 229} 230``` 231 232## systemparameter.set 233 234set(key: string, value: string, callback: AsyncCallback<void>): void 235 236设置系统参数Key对应的值。 237 238**系统能力:** SystemCapability.Startup.SystemInfo 239 240**参数:** 241 242| 参数名 | 类型 | 必填 | 说明 | 243| -------- | -------- | -------- | -------- | 244| key | string | 是 | 待设置的系统参数Key。 | 245| value | string | 是 | 待设置的系统参数值。 | 246| callback | AsyncCallback<void> | 是 | 回调函数。 | 247 248**错误码**: 249 250| 错误码ID | 错误信息 | 251| -------- | ------------------------------------------------------------ | 252| 14700102 | if value is invalid | 253| 14700103 | if permission denied | 254| 14700104 | if system internal error | 255 256以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 257 258**示例:** 259 260```ts 261import { BusinessError } from '@ohos.base'; 262 263try { 264 systemparameter.set("test.parameter.key", "testValue", (err: BusinessError, data: void) => { 265 if (err == undefined) { 266 console.log("set test.parameter.key value success :" + data) 267 } else { 268 console.log("set test.parameter.key value err:" + err.code) 269 }}); 270} catch(e) { 271 console.log("set unexpected error: " + e); 272} 273``` 274 275## systemparameter.set 276 277set(key: string, value: string): Promise<void> 278 279设置系统参数Key对应的值。 280 281**系统能力:** SystemCapability.Startup.SystemInfo 282 283**参数:** 284 285| 参数名 | 类型 | 必填 | 说明 | 286| -------- | -------- | -------- | -------- | 287| key | string | 是 | 待设置的系统参数Key。 | 288| value| string | 是 | 待设置的系统参数值。 | 289 290**返回值:** 291 292| 类型 | 说明 | 293| -------- | -------- | 294| Promise<void> | Promise示例,用于异步获取结果。 | 295 296**错误码**: 297 298| 错误码ID | 错误信息 | 299| -------- | ------------------------------------------------------------ | 300| 14700102 | if value is invalid | 301| 14700103 | if permission denied | 302| 14700104 | if system internal error | 303 304以上错误码详细介绍请参考[errorcode-system-parameterV9](../errorcodes/errorcode-system-parameterV9.md)。 305 306**示例:** 307 308```ts 309import { BusinessError } from '@ohos.base'; 310 311try { 312 let p: Promise<void> = systemparameter.set("test.parameter.key", "testValue"); 313 p.then((value: void) => { 314 console.log("set test.parameter.key success: " + value); 315 }).catch((err: BusinessError) => { 316 console.log(" set test.parameter.key error: " + err.code); 317 }); 318} catch(e) { 319 console.log("set unexpected error: " + e); 320} 321``` 322