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