1# @ohos.systemParameter (System Parameter) 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 5[Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md). 6 7> **NOTE** 8> - 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.md) instead. 9> - 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. 10> - The APIs provided by this module are system APIs. 11> - Third-party applications cannot use the APIs provided by this module, because system parameters each require specific discretionary access control (DAC) and MAC permissions. 12 13 14## Modules to Import 15 16```ts 17import systemparameter from '@ohos.systemparameter' 18``` 19 20## systemparameter.getSync<sup>(deprecated)</sup> 21 22getSync(key: string, def?: string): string 23 24Obtains the value of the system parameter with the specified key. 25 26**System capability**: SystemCapability.Startup.SystemInfo 27 28**Parameters** 29 30| Name| Type| Mandatory| Description| 31| -------- | -------- | -------- | -------- | 32| key | string | Yes| Key of the system parameter.| 33| def | string | No| Default value.| 34 35**Return value** 36 37| Type| Description| 38| -------- | -------- | 39| string | Value of the system parameter. If the specified key does not exist, the default value is returned. If no default value has been set, an empty string will be returned.| 40 41**Example** 42 43```ts 44try { 45 var info = systemparameter.getSync("const.ohos.apiversion"); 46 console.log(JSON.stringify(info)); 47}catch(e){ 48 console.log("getSync unexpected error: " + e); 49} 50``` 51 52## systemparameter.get<sup>(deprecated)</sup> 53 54get(key: string, callback: AsyncCallback<string>): void 55 56Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result. 57 58**System capability**: SystemCapability.Startup.SystemInfo 59 60**Parameters** 61 62| Name| Type| Mandatory| Description| 63| -------- | -------- | -------- | -------- | 64| key | string | Yes| Key of the system parameter.| 65| callback | AsyncCallback<string> | Yes| Callback used to return the result.| 66 67**Example** 68 69```ts 70try { 71 systemparameter.get("const.ohos.apiversion", function (err, data) { 72 if (err == undefined) { 73 console.log("get test.parameter.key value success:" + data) 74 } else { 75 console.log(" get test.parameter.key value err:" + err.code) 76 }}); 77}catch(e){ 78 console.log("get unexpected error: " + e); 79} 80``` 81 82## systemparameter.get<sup>(deprecated)</sup> 83 84get(key: string, def: string, callback: AsyncCallback<string>): void 85 86Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result. 87 88**System capability**: SystemCapability.Startup.SystemInfo 89 90**Parameters** 91 92| Name| Type| Mandatory| Description| 93| -------- | -------- | -------- | -------- | 94| key | string | Yes| Key of the system parameter.| 95| def | string | Yes| Default value.| 96| callback | AsyncCallback<string> | Yes| Callback used to return the result.| 97 98**Example** 99 100```ts 101try { 102 systemparameter.get("const.ohos.apiversion", "default", function (err, data) { 103 if (err == undefined) { 104 console.log("get test.parameter.key value success:" + data) 105 } else { 106 console.log(" get test.parameter.key value err:" + err.code) 107 } 108 }); 109}catch(e){ 110 console.log("get unexpected error:" + e) 111} 112``` 113 114## systemparameter.get<sup>(deprecated)</sup> 115 116get(key: string, def?: string): Promise<string> 117 118Obtains the value of the system parameter with the specified key. This API uses a promise to return the result. 119 120**System capability**: SystemCapability.Startup.SystemInfo 121 122**Parameters** 123 124| Name| Type| Mandatory| Description| 125| -------- | -------- | -------- | -------- | 126| key | string | Yes| Key of the system parameter.| 127| def | string | No| Default value.| 128 129**Return value** 130 131| Type| Description| 132| -------- | -------- | 133| Promise<string> | Promise used to return the execution result.| 134 135**Example** 136 137```ts 138try { 139 var p = systemparameter.get("const.ohos.apiversion"); 140 p.then(function (value) { 141 console.log("get test.parameter.key success: " + value); 142 }).catch(function (err) { 143 console.log("get test.parameter.key error: " + err.code); 144 }); 145}catch(e){ 146 console.log("get unexpected error: " + e); 147} 148``` 149 150## systemparameter.setSync<sup>(deprecated)</sup> 151 152setSync(key: string, value: string): void 153 154Sets a value for the system parameter with the specified key. 155 156**System capability**: SystemCapability.Startup.SystemInfo 157 158**Parameters** 159 160| Name| Type| Mandatory| Description| 161| -------- | -------- | -------- | -------- | 162| key | string | Yes| Key of the system parameter.| 163| value | string | Yes| Value of the system parameter to set.| 164 165> **NOTE** 166> - This API can be used only for setting parameters of system applications. 167> - 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). 168 169 170**Example** 171 172```ts 173try { 174 systemparameter.setSync("test.parameter.key", "default"); 175}catch(e){ 176 console.log("set unexpected error: " + e); 177} 178``` 179 180## systemparameter.set<sup>(deprecated)</sup> 181 182set(key: string, value: string, callback: AsyncCallback<void>): void 183 184Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result. 185 186**System capability**: SystemCapability.Startup.SystemInfo 187 188**Parameters** 189 190| Name| Type| Mandatory| Description| 191| -------- | -------- | -------- | -------- | 192| key | string | Yes| Key of the system parameter.| 193| value | string | Yes| Value of the system parameter to set.| 194| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 195 196> **NOTE** 197> - This API can be used only for setting parameters of system applications. 198> - 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). 199 200**Example** 201 202```ts 203try { 204 systemparameter.set("test.parameter.key", "testValue", function (err, data) { 205 if (err == undefined) { 206 console.log("set test.parameter.key value success :" + data) 207 } else { 208 console.log("set test.parameter.key value err:" + err.code) 209 }}); 210}catch(e){ 211 console.log("set unexpected error: " + e); 212} 213``` 214 215## systemparameter.set<sup>(deprecated)</sup> 216 217set(key: string, value: string): Promise<void> 218 219Sets a value for the system parameter with the specified key. This API uses a promise to return the result. 220 221**System capability**: SystemCapability.Startup.SystemInfo 222 223**Parameters** 224 225| Name| Type| Mandatory| Description| 226| -------- | -------- | -------- | -------- | 227| key | string | Yes| Key of the system parameter.| 228| value| string | Yes| Value of the system parameter to set.| 229 230**Return value** 231 232| Type| Description| 233| -------- | -------- | 234| Promise<void> | Promise used to return the execution result.| 235 236> **NOTE** 237> - This API can be used only for setting parameters of system applications. 238> - 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). 239 240**Example** 241 242```ts 243try { 244 var p = systemparameter.set("test.parameter.key", "testValue"); 245 p.then(function (value) { 246 console.log("set test.parameter.key success: " + value); 247 }).catch(function (err) { 248 console.log(" set test.parameter.key error: " + err.code); 249 }); 250}catch(e){ 251 console.log("set unexpected error: " + e); 252} 253``` 254