• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5For details about the system parameter design principles and definitions, see [Parameter 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 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.|
34
35**Return value**
36
37| Type| Description|
38| -------- | -------- |
39| 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.|
40
41**Example**
42
43```ts
44try {
45    let info: string = 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&lt;string&gt;): 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&lt;string&gt; | Yes| Callback used to return the result.|
66
67**Example**
68
69```ts
70import { BusinessError } from '@ohos.base';
71
72try {
73    systemparameter.get("const.ohos.apiversion", (err: BusinessError, data: string) => {
74    if (err == undefined) {
75        console.log("get test.parameter.key value success:" + data)
76    } else {
77        console.log(" get test.parameter.key value err:" + err.code)
78    }});
79} catch(e) {
80    console.log("get unexpected error: " + e);
81}
82```
83
84## systemparameter.get<sup>(deprecated)</sup>
85
86get(key: string, def: string, callback: AsyncCallback&lt;string&gt;): void
87
88Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result.
89
90**System capability**: SystemCapability.Startup.SystemInfo
91
92**Parameters**
93
94| Name| Type| Mandatory| Description|
95| -------- | -------- | -------- | -------- |
96| key | string | Yes| Key of the system parameter.|
97| def | string | Yes| Default value.|
98| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
99
100**Example**
101
102```ts
103import { BusinessError } from '@ohos.base';
104
105try {
106    systemparameter.get("const.ohos.apiversion", "default", (err: BusinessError, data: string) => {
107        if (err == undefined) {
108            console.log("get test.parameter.key value success:" + data)
109        } else {
110            console.log(" get test.parameter.key value err:" + err.code)
111        }
112    });
113} catch(e) {
114    console.log("get unexpected error:" + e)
115}
116```
117
118## systemparameter.get<sup>(deprecated)</sup>
119
120get(key: string, def?: string): Promise&lt;string&gt;
121
122Obtains the value of the system parameter with the specified key. This API uses a promise to return the result.
123
124**System capability**: SystemCapability.Startup.SystemInfo
125
126**Parameters**
127
128| Name| Type| Mandatory| Description|
129| -------- | -------- | -------- | -------- |
130| key | string | Yes| Key of the system parameter.|
131| 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.|
132
133**Return value**
134
135| Type| Description|
136| -------- | -------- |
137| Promise&lt;string&gt; | Promise used to return the execution result.|
138
139**Example**
140
141```ts
142import { BusinessError } from '@ohos.base';
143
144try {
145    let p: Promise<string> = systemparameter.get("const.ohos.apiversion");
146    p.then((value: string) => {
147        console.log("get test.parameter.key success: " + value);
148    }).catch((err: BusinessError) => {
149        console.log("get test.parameter.key error: " + err.code);
150    });
151} catch(e) {
152    console.log("get unexpected error: " + e);
153}
154```
155
156## systemparameter.setSync<sup>(deprecated)</sup>
157
158setSync(key: string, value: string): void
159
160Sets a value for the system parameter with the specified key.
161
162**System capability**: SystemCapability.Startup.SystemInfo
163
164**Parameters**
165
166| Name| Type| Mandatory| Description|
167| -------- | -------- | -------- | -------- |
168| key | string | Yes| Key of the system parameter.|
169| value | string | Yes| Value of the system parameter to set.|
170
171> **NOTE**
172> - This API can be used only for setting parameters of system applications.
173> - 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).
174
175
176**Example**
177
178```ts
179try {
180    systemparameter.setSync("test.parameter.key", "default");
181} catch(e) {
182    console.log("set unexpected error: " + e);
183}
184```
185
186## systemparameter.set<sup>(deprecated)</sup>
187
188set(key: string, value: string, callback: AsyncCallback&lt;void&gt;): void
189
190Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result.
191
192**System capability**: SystemCapability.Startup.SystemInfo
193
194**Parameters**
195
196| Name| Type| Mandatory| Description|
197| -------- | -------- | -------- | -------- |
198| key | string | Yes| Key of the system parameter.|
199| value | string | Yes| Value of the system parameter to set.|
200| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
201
202> **NOTE**
203> - This API can be used only for setting parameters of system applications.
204> - 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).
205
206**Example**
207
208```ts
209import { BusinessError } from '@ohos.base';
210
211try {
212    systemparameter.set("test.parameter.key", "testValue",  (err: BusinessError, data: void) =>{
213    if (err == undefined) {
214        console.log("set test.parameter.key value success :" + data)
215    } else {
216        console.log("set test.parameter.key value err:" + err.code)
217    }});
218} catch(e) {
219    console.log("set unexpected error: " + e);
220}
221```
222
223## systemparameter.set<sup>(deprecated)</sup>
224
225set(key: string, value: string): Promise&lt;void&gt;
226
227Sets a value for the system parameter with the specified key. This API uses a promise to return the result.
228
229**System capability**: SystemCapability.Startup.SystemInfo
230
231**Parameters**
232
233| Name| Type| Mandatory| Description|
234| -------- | -------- | -------- | -------- |
235| key | string | Yes| Key of the system parameter.|
236| value| string | Yes| Value of the system parameter to set.|
237
238**Return value**
239
240| Type| Description|
241| -------- | -------- |
242| Promise&lt;void&gt; | Promise used to return the execution result.|
243
244> **NOTE**
245> - This API can be used only for setting parameters of system applications.
246> - 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).
247
248**Example**
249
250```ts
251import { BusinessError } from '@ohos.base';
252
253try {
254    let p: Promise<void> = systemparameter.set("test.parameter.key", "testValue");
255    p.then((value: void) => {
256        console.log("set test.parameter.key success: " + value);
257    }).catch((err: BusinessError) => {
258        console.log(" set test.parameter.key error: " + err.code);
259    });
260} catch(e) {
261    console.log("set unexpected error: " + e);
262}
263```
264