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