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 { systemParameterEnhance } from '@kit.BasicServicesKit'; 16``` 17 18## systemParameterEnhance.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。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 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| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 44| 14700101 | System parameter not found. | 45| 14700103 | The operation on the system permission is denied. | 46| 14700104 | System internal error such as out memory or deadlock. | 47 48以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 49 50**示例:** 51 52```ts 53try { 54 let info: string = systemParameterEnhance.getSync("const.ohos.apiversion"); 55 console.log(JSON.stringify(info)); 56} catch(e) { 57 console.log("getSync unexpected error: " + e); 58} 59``` 60 61## systemParameterEnhance.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。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 74| callback | AsyncCallback<string> | 是 | 回调函数。 | 75 76**错误码**: 77 78| 错误码ID | 错误信息 | 79| -------- | ------------------------------------------------------------ | 80| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 81| 14700101 | System parameter not found. | 82| 14700103 | The operation on the system permission is denied. | 83| 14700104 | System internal error such as out memory or deadlock. | 84 85以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 86 87**示例:** 88 89```ts 90import { BusinessError } from '@kit.BasicServicesKit'; 91 92try { 93 systemParameterEnhance.get("const.ohos.apiversion", (err: BusinessError, data: string) => { 94 if (err == undefined) { 95 console.log("get test.parameter.key value success:" + data) 96 } else { 97 console.log(" get test.parameter.key value err:" + err.code) 98 }}); 99} catch(e) { 100 console.log("get unexpected error: " + e); 101} 102``` 103 104## systemParameterEnhance.get 105 106get(key: string, def: string, callback: AsyncCallback<string>): void 107 108获取系统参数Key对应的值。 109 110**系统能力:** SystemCapability.Startup.SystemInfo 111 112**参数:** 113 114| 参数名 | 类型 | 必填 | 说明 | 115| -------- | -------- | -------- | -------- | 116| key | string | 是 | 待查询的系统参数Key。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 117| def | string | 是 | 默认值。 | 118| callback | AsyncCallback<string> | 是 | 回调函数。 | 119 120**错误码**: 121 122| 错误码ID | 错误信息 | 123| -------- | ------------------------------------------------------------ | 124| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 125| 14700101 | System parameter not found. | 126| 14700103 | The operation on the system permission is denied. | 127| 14700104 | System internal error such as out memory or deadlock. | 128 129以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 130 131**示例:** 132 133```ts 134import { BusinessError } from '@kit.BasicServicesKit'; 135 136try { 137 systemParameterEnhance.get("const.ohos.apiversion", "default", (err: BusinessError, data: string) => { 138 if (err == undefined) { 139 console.log("get test.parameter.key value success:" + data) 140 } else { 141 console.log(" get test.parameter.key value err:" + err.code) 142 } 143 }); 144} catch(e) { 145 console.log("get unexpected error:" + e) 146} 147``` 148 149## systemParameterEnhance.get 150 151get(key: string, def?: string): Promise<string> 152 153获取系统参数Key对应的值。 154 155**系统能力:** SystemCapability.Startup.SystemInfo 156 157**参数:** 158 159| 参数名 | 类型 | 必填 | 说明 | 160| -------- | -------- | -------- | -------- | 161| key | string | 是 | 待查询的系统参数Key。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 162| def | string | 否 | def为所要获取的系统参数的默认值 <br> def为可选参数,仅当系统参数不存在时生效 <br> def可以传undefined或自定义的任意值 | 163 164**返回值:** 165 166| 类型 | 说明 | 167| -------- | -------- | 168| Promise<string> | Promise示例,用于异步获取结果。 | 169 170**错误码**: 171 172| 错误码ID | 错误信息 | 173| -------- | ------------------------------------------------------------ | 174| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 175| 14700101 | System parameter not found. | 176| 14700103 | The operation on the system permission is denied. | 177| 14700104 | System internal error such as out memory or deadlock. | 178 179以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 180 181**示例:** 182 183```ts 184import { BusinessError } from '@kit.BasicServicesKit'; 185 186try { 187 let p: Promise<string> = systemParameterEnhance.get("const.ohos.apiversion"); 188 p.then((value: string) => { 189 console.log("get test.parameter.key success: " + value); 190 }).catch((err: BusinessError) => { 191 console.log("get test.parameter.key error: " + err.code); 192 }); 193} catch(e) { 194 console.log("get unexpected error: " + e); 195} 196``` 197 198## systemParameterEnhance.setSync 199 200setSync(key: string, value: string): void 201 202设置系统参数Key对应的值。 203 204**系统能力:** SystemCapability.Startup.SystemInfo 205 206**参数:** 207 208| 参数名 | 类型 | 必填 | 说明 | 209| -------- | -------- | -------- | -------- | 210| key | string | 是 | 待设置的系统参数Key。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 211| value | string | 是 | 待设置的系统参数值。最大长度96字节(包括结束符) | 212 213**错误码**: 214 215| 错误码ID | 错误信息 | 216| -------- | ------------------------------------------------------------ | 217| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 218| 14700102 | Invalid system parameter value. | 219| 14700103 | The operation on the system permission is denied. | 220| 14700104 | System internal error such as out memory or deadlock. | 221 222以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 223 224**示例:** 225 226```ts 227import { BusinessError } from '@kit.BasicServicesKit'; 228 229try { 230 systemParameterEnhance.setSync("test.parameter.key", "default"); 231} catch(e) { 232 console.log("set unexpected error: " + e); 233} 234``` 235 236## systemParameterEnhance.set 237 238set(key: string, value: string, callback: AsyncCallback<void>): void 239 240设置系统参数Key对应的值。 241 242**系统能力:** SystemCapability.Startup.SystemInfo 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| -------- | -------- | -------- | -------- | 248| key | string | 是 | 待设置的系统参数Key。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 249| value | string | 是 | 待设置的系统参数值。最大长度96字节(包括结束符) | 250| callback | AsyncCallback<void> | 是 | 回调函数。 | 251 252**错误码**: 253 254| 错误码ID | 错误信息 | 255| -------- | ------------------------------------------------------------ | 256| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 257| 14700102 | Invalid system parameter value. | 258| 14700103 | The operation on the system permission is denied. | 259| 14700104 | System internal error such as out memory or deadlock. | 260 261以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 262 263**示例:** 264 265```ts 266import { BusinessError } from '@kit.BasicServicesKit'; 267 268try { 269 systemParameterEnhance.set("test.parameter.key", "testValue", (err: BusinessError, data: void) => { 270 if (err == undefined) { 271 console.log("set test.parameter.key value success :" + data) 272 } else { 273 console.log("set test.parameter.key value err:" + err.code) 274 }}); 275} catch(e) { 276 console.log("set unexpected error: " + e); 277} 278``` 279 280## systemParameterEnhance.set 281 282set(key: string, value: string): Promise<void> 283 284设置系统参数Key对应的值。 285 286**系统能力:** SystemCapability.Startup.SystemInfo 287 288**参数:** 289 290| 参数名 | 类型 | 必填 | 说明 | 291| -------- | -------- | -------- | -------- | 292| key | string | 是 | 待设置的系统参数Key。最大长度128字节,只允许字母数字加".","-","@",":"或"_",不允许".."。 | 293| value| string | 是 | 待设置的系统参数值。最大长度96字节(包括结束符) | 294 295**返回值:** 296 297| 类型 | 说明 | 298| -------- | -------- | 299| Promise<void> | Promise示例,用于异步获取结果。 | 300 301**错误码**: 302 303| 错误码ID | 错误信息 | 304| -------- | ------------------------------------------------------------ | 305| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.incorrect parameter types; 3.parameter verification failed. | 306| 14700102 | Invalid system parameter value. | 307| 14700103 | The operation on the system permission is denied. | 308| 14700104 | System internal error such as out memory or deadlock. | 309 310以上错误码详细介绍请参考[系统参数错误码](errorcode-system-parameterV9.md)。 311 312**示例:** 313 314```ts 315import { BusinessError } from '@kit.BasicServicesKit'; 316 317try { 318 let p: Promise<void> = systemParameterEnhance.set("test.parameter.key", "testValue"); 319 p.then((value: void) => { 320 console.log("set test.parameter.key success: " + value); 321 }).catch((err: BusinessError) => { 322 console.log(" set test.parameter.key error: " + err.code); 323 }); 324} catch(e) { 325 console.log("set unexpected error: " + e); 326} 327``` 328