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