1# @ohos.systemParameterEnhance (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> 9> - The initial APIs of this module are supported since API version 9. 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 mandatory access control (MAC) permissions. 12 13## Modules to Import 14 15```ts 16import systemparameter from '@ohos.systemParameterEnhance' 17``` 18 19## systemparameter.getSync 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.| 33 34**Return value** 35 36| Type| Description| 37| -------- | -------- | 38| 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.| 39 40**Example** 41 42```ts 43try { 44 var info = 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 52 53get(key: string, callback: AsyncCallback<string>): void 54 55Obtains the value of the system parameter with the specified key. 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 69try { 70 systemparameter.get("const.ohos.apiversion", function (err, data) { 71 if (err == undefined) { 72 console.log("get test.parameter.key value success:" + data) 73 } else { 74 console.log(" get test.parameter.key value err:" + err.code) 75 }}); 76}catch(e){ 77 console.log("get unexpected error: " + e); 78} 79``` 80 81## systemparameter.get 82 83get(key: string, def: string, callback: AsyncCallback<string>): void 84 85Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result. 86 87**System capability**: SystemCapability.Startup.SystemInfo 88 89**Parameters** 90 91| Name| Type| Mandatory| Description| 92| -------- | -------- | -------- | -------- | 93| key | string | Yes| Key of the system parameter.| 94| def | string | Yes| Default value.| 95| callback | AsyncCallback<string> | Yes| Callback used to return the result.| 96 97**Example** 98 99```ts 100try { 101 systemparameter.get("const.ohos.apiversion", "default", function (err, data) { 102 if (err == undefined) { 103 console.log("get test.parameter.key value success:" + data) 104 } else { 105 console.log(" get test.parameter.key value err:" + err.code) 106 } 107 }); 108}catch(e){ 109 console.log("get unexpected error:" + e) 110} 111``` 112 113## systemparameter.get 114 115get(key: string, def?: string): Promise<string> 116 117Obtains the value of the system parameter with the specified key. This API uses a promise to return the result. 118 119**System capability**: SystemCapability.Startup.SystemInfo 120 121**Parameters** 122 123| Name| Type| Mandatory| Description| 124| -------- | -------- | -------- | -------- | 125| key | string | Yes| Key of the system parameter.| 126| def | string | No| Default value.| 127 128**Return value** 129 130| Type| Description| 131| -------- | -------- | 132| Promise<string> | Promise used to return the execution result.| 133 134**Example** 135 136```ts 137try { 138 var p = systemparameter.get("const.ohos.apiversion"); 139 p.then(function (value) { 140 console.log("get test.parameter.key success: " + value); 141 }).catch(function (err) { 142 console.log("get test.parameter.key error: " + err.code); 143 }); 144}catch(e){ 145 console.log("get unexpected error: " + e); 146} 147``` 148 149## systemparameter.setSync 150 151setSync(key: string, value: string): void 152 153Sets a value for the system parameter with the specified key. 154 155**System capability**: SystemCapability.Startup.SystemInfo 156 157**Parameters** 158 159| Name| Type| Mandatory| Description| 160| -------- | -------- | -------- | -------- | 161| key | string | Yes| Key of the system parameter.| 162| value | string | Yes| Value of the system parameter to set.| 163 164**Example** 165 166```ts 167try { 168 systemparameter.setSync("test.parameter.key", "default"); 169}catch(e){ 170 console.log("set unexpected error: " + e); 171} 172``` 173 174## systemparameter.set 175 176set(key: string, value: string, callback: AsyncCallback<void>): void 177 178Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result. 179 180**System capability**: SystemCapability.Startup.SystemInfo 181 182**Parameters** 183 184| Name| Type| Mandatory| Description| 185| -------- | -------- | -------- | -------- | 186| key | string | Yes| Key of the system parameter.| 187| value | string | Yes| Value of the system parameter to set.| 188| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 189 190**Example** 191 192```ts 193try { 194 systemparameter.set("test.parameter.key", "testValue", function (err, data) { 195 if (err == undefined) { 196 console.log("set test.parameter.key value success :" + data) 197 } else { 198 console.log("set test.parameter.key value err:" + err.code) 199 }}); 200}catch(e){ 201 console.log("set unexpected error: " + e); 202} 203``` 204 205## systemparameter.set 206 207set(key: string, value: string): Promise<void> 208 209Sets a value for the system parameter with the specified key. This API uses a promise to return the result. 210 211**System capability**: SystemCapability.Startup.SystemInfo 212 213**Parameters** 214 215| Name| Type| Mandatory| Description| 216| -------- | -------- | -------- | -------- | 217| key | string | Yes| Key of the system parameter.| 218| value| string | Yes| Value of the system parameter to set.| 219 220**Return value** 221 222| Type| Description| 223| -------- | -------- | 224| Promise<void> | Promise used to return the execution result.| 225 226**Example** 227 228```ts 229try { 230 var p = systemparameter.set("test.parameter.key", "testValue"); 231 p.then(function (value) { 232 console.log("set test.parameter.key success: " + value); 233 }).catch(function (err) { 234 console.log(" set test.parameter.key error: " + err.code); 235 }); 236}catch(e){ 237 console.log("set unexpected error: " + e); 238} 239``` 240