1# @ohos.systemParameter (System Parameter) (System API) 2 3The **SystemParameter** module provides system services with easy access to key-value pairs. You can use the APIs provided by this module to describe the service status and change the service behavior. The basic operation primitives are get and set. You can obtain the values of system parameters through getters and modify the values through setters. 4For details about the system parameter design principles and definitions, see [Parameter Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md). 5 6> **NOTE** 7> - The APIs of this module are no longer maintained since API version 9. It is recommended that you use [@ohos.systemParameterEnhance](js-apis-system-parameterEnhance-sys.md) instead. 8> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 9> - The APIs provided by this module are system APIs. 10> - Third-party applications cannot use the APIs provided by this module, because system parameters each require specific discretionary access control (DAC) and MAC permissions. 11 12 13## Modules to Import 14 15```ts 16import systemparameter from '@ohos.systemparameter'; 17``` 18 19## systemparameter.getSync<sup>(deprecated)</sup> 20 21getSync(key: string, def?: string): string 22 23Obtains the value of the system parameter with the specified key. 24 25**System capability**: SystemCapability.Startup.SystemInfo 26 27**Parameters** 28 29| Name| Type| Mandatory| Description| 30| -------- | -------- | -------- | -------- | 31| key | string | Yes| Key of the system parameter.| 32| def | string | No| Default value of the system parameter.<br> It works only when the system parameter does not exist.<br>The value can be **undefined** or any custom value.| 33 34**Return value** 35 36| Type| Description| 37| -------- | -------- | 38| string | Value of the system parameter.<br> If the specified key exists, the set value is returned.<br> If the specified key does not exist and **def** is set to a valid value, the set value is returned. If the specified key does not exist and **def** is set to an invalid value (such as **undefined**) or is not set, an empty string is returned.| 39 40**Example** 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 55Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result. 56 57**System capability**: SystemCapability.Startup.SystemInfo 58 59**Parameters** 60 61| Name| Type| Mandatory| Description| 62| -------- | -------- | -------- | -------- | 63| key | string | Yes| Key of the system parameter.| 64| callback | AsyncCallback<string> | Yes| Callback used to return the result.| 65 66**Example** 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 87Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result. 88 89**System capability**: SystemCapability.Startup.SystemInfo 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| key | string | Yes| Key of the system parameter.| 96| def | string | Yes| Default value.| 97| callback | AsyncCallback<string> | Yes| Callback used to return the result.| 98 99**Example** 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 121Obtains the value of the system parameter with the specified key. This API uses a promise to return the result. 122 123**System capability**: SystemCapability.Startup.SystemInfo 124 125**Parameters** 126 127| Name| Type| Mandatory| Description| 128| -------- | -------- | -------- | -------- | 129| key | string | Yes| Key of the system parameter.| 130| def | string | No| Default value of the system parameter.<br> It works only when the system parameter does not exist.<br> The value can be **undefined** or any custom value.| 131 132**Return value** 133 134| Type| Description| 135| -------- | -------- | 136| Promise<string> | Promise used to return the execution result.| 137 138**Example** 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 159Sets a value for the system parameter with the specified key. 160 161**System capability**: SystemCapability.Startup.SystemInfo 162 163**Parameters** 164 165| Name| Type| Mandatory| Description| 166| -------- | -------- | -------- | -------- | 167| key | string | Yes| Key of the system parameter.| 168| value | string | Yes| Value of the system parameter to set.| 169 170> **NOTE** 171> - This API can be used only for setting parameters of system applications. 172> - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see [Parameter Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md). 173 174 175**Example** 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 189Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result. 190 191**System capability**: SystemCapability.Startup.SystemInfo 192 193**Parameters** 194 195| Name| Type| Mandatory| Description| 196| -------- | -------- | -------- | -------- | 197| key | string | Yes| Key of the system parameter.| 198| value | string | Yes| Value of the system parameter to set.| 199| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 200 201> **NOTE** 202> - This API can be used only for setting parameters of system applications. 203> - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see [Parameter Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md). 204 205**Example** 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 226Sets a value for the system parameter with the specified key. This API uses a promise to return the result. 227 228**System capability**: SystemCapability.Startup.SystemInfo 229 230**Parameters** 231 232| Name| Type| Mandatory| Description| 233| -------- | -------- | -------- | -------- | 234| key | string | Yes| Key of the system parameter.| 235| value| string | Yes| Value of the system parameter to set.| 236 237**Return value** 238 239| Type| Description| 240| -------- | -------- | 241| Promise<void> | Promise used to return the execution result.| 242 243> **NOTE** 244> - This API can be used only for setting parameters of system applications. 245> - SELinux and Discretionary Access Control (DAC) rules must be configured for authorized system applications. For details about how to configure SELinux and DAC rules, see [Parameter Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md). 246 247**Example** 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