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