js-apis-system-parameter.md 8.1 KB
Newer Older
E
ester.zhou 已提交
1
# SystemParameter
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5 6 7 8 9
The **SystemParameter** module provides system services with easy access to key-value pairs. You can use the APIs of 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.
For details about the system parameter design principles and definitions, see
[Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md).

> **NOTE**
>
> - The APIs of this module are no longer maintained since API version 9. It is recommended that you use [@ohos.systemParameterV9](js-apis-system-parameterV9.md) instead.
E
esterzhou 已提交
10
> - 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.
E
ester.zhou 已提交
11 12 13
> - The APIs provided by this module are system APIs.
> - Third-party applications cannot use the APIs provided by this module, because system parameters each require specific discretionary access control (DAC) and MAC permissions.

E
esterzhou 已提交
14 15

## Modules to Import
Z
zengyawen 已提交
16

P
update  
Peter_1988 已提交
17
```ts
E
ester.zhou 已提交
18
import systemparameter from '@ohos.systemparameter'
Z
zengyawen 已提交
19 20
```

E
ester.zhou 已提交
21
## systemparameter.getSync<sup>(deprecated)</sup>
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23
getSync(key: string, def?: string): string
Z
zengyawen 已提交
24

E
ester.zhou 已提交
25
Obtains the value of the system parameter with the specified key.
Z
zengyawen 已提交
26

E
ester.zhou 已提交
27
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
28 29 30

**Parameters**

E
esterzhou 已提交
31 32
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
33
| key | string | Yes| Key of the system parameter.|
E
ester.zhou 已提交
34
| def | string | No| Default value.|
E
esterzhou 已提交
35 36 37 38 39

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
40
| 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.|
Z
zengyawen 已提交
41 42 43

**Example**

P
update  
Peter_1988 已提交
44
```ts
Z
zengyawen 已提交
45
try {
E
ester.zhou 已提交
46
    var info = systemparameter.getSync("const.ohos.apiversion");
Z
zengyawen 已提交
47 48 49 50 51 52
    console.log(JSON.stringify(info));
}catch(e){
    console.log("getSync unexpected error: " + e);
}
```

E
ester.zhou 已提交
53
## systemparameter.get<sup>(deprecated)</sup>
Z
zengyawen 已提交
54

E
ester.zhou 已提交
55
get(key: string, callback: AsyncCallback&lt;string&gt;): void
E
esterzhou 已提交
56

E
ester.zhou 已提交
57
Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
58

E
ester.zhou 已提交
59
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
60 61 62

**Parameters**

E
esterzhou 已提交
63 64
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
65
| key | string | Yes| Key of the system parameter.|
E
esterzhou 已提交
66
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
67 68 69

**Example**

P
update  
Peter_1988 已提交
70
```ts
Z
zengyawen 已提交
71
try {
E
ester.zhou 已提交
72
    systemparameter.get("const.ohos.apiversion", function (err, data) {
Z
zengyawen 已提交
73 74 75 76 77 78 79 80 81 82
    if (err == undefined) {
        console.log("get test.parameter.key value success:" + data)
    } else {
        console.log(" get test.parameter.key value err:" + err.code)
    }});
}catch(e){
    console.log("get unexpected error: " + e);
}
```

E
ester.zhou 已提交
83
## systemparameter.get<sup>(deprecated)</sup>
E
esterzhou 已提交
84

E
ester.zhou 已提交
85
get(key: string, def: string, callback: AsyncCallback&lt;string&gt;): void
Z
zengyawen 已提交
86

E
ester.zhou 已提交
87
Obtains the value of the system parameter with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
88

E
ester.zhou 已提交
89
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
90 91 92

**Parameters**

E
esterzhou 已提交
93 94
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
95
| key | string | Yes| Key of the system parameter.|
E
ester.zhou 已提交
96
| def | string | Yes| Default value.|
E
esterzhou 已提交
97
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
98 99 100

**Example**

P
update  
Peter_1988 已提交
101
```ts
Z
zengyawen 已提交
102
try {
E
ester.zhou 已提交
103
    systemparameter.get("const.ohos.apiversion", "default", function (err, data) {
Z
zengyawen 已提交
104 105 106 107 108 109 110 111 112 113 114
        if (err == undefined) {
            console.log("get test.parameter.key value success:" + data)
        } else {
            console.log(" get test.parameter.key value err:" + err.code)
        }
    });
}catch(e){
    console.log("get unexpected error:" + e)
}
```

E
ester.zhou 已提交
115
## systemparameter.get<sup>(deprecated)</sup>
E
esterzhou 已提交
116

E
ester.zhou 已提交
117
get(key: string, def?: string): Promise&lt;string&gt;
E
esterzhou 已提交
118

E
ester.zhou 已提交
119
Obtains the value of the system parameter with the specified key. This API uses a promise to return the result.
Z
zengyawen 已提交
120

E
ester.zhou 已提交
121
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
122 123 124

**Parameters**

E
esterzhou 已提交
125 126
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
127
| key | string | Yes| Key of the system parameter.|
E
ester.zhou 已提交
128
| def | string | No| Default value.|
E
esterzhou 已提交
129 130 131 132 133 134

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;string&gt; | Promise used to return the execution result.|
Z
zengyawen 已提交
135 136 137

**Example**

P
update  
Peter_1988 已提交
138
```ts
Z
zengyawen 已提交
139
try {
E
ester.zhou 已提交
140
    var p = systemparameter.get("const.ohos.apiversion");
Z
zengyawen 已提交
141 142 143 144 145 146 147 148 149 150
    p.then(function (value) {
        console.log("get test.parameter.key success: " + value);
    }).catch(function (err) {
        console.log("get test.parameter.key error: " + err.code);
    });
}catch(e){
    console.log("get unexpected error: " + e);
}
```

E
ester.zhou 已提交
151
## systemparameter.setSync<sup>(deprecated)</sup>
E
esterzhou 已提交
152

E
ester.zhou 已提交
153
setSync(key: string, value: string): void
Z
zengyawen 已提交
154

E
ester.zhou 已提交
155
Sets a value for the system parameter with the specified key.
Z
zengyawen 已提交
156

E
ester.zhou 已提交
157
**System capability**: SystemCapability.Startup.SystemInfo
E
esterzhou 已提交
158

Z
zengyawen 已提交
159 160
**Parameters**

E
esterzhou 已提交
161 162
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
163 164 165 166 167 168 169
| key | string | Yes| Key of the system parameter.|
| value | string | Yes| Value of the system parameter to set.|

> **NOTE**
>
> - This API can be used only for setting parameters of system applications.
> - SELinux and DAC rules must be configured for authorized system applications. For details, see [Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md).
Z
zengyawen 已提交
170 171 172

**Example**

P
update  
Peter_1988 已提交
173
```ts
Z
zengyawen 已提交
174
try {
E
ester.zhou 已提交
175
    systemparameter.setSync("test.parameter.key", "default");
Z
zengyawen 已提交
176 177 178 179 180
}catch(e){
    console.log("set unexpected error: " + e);
}
```

E
ester.zhou 已提交
181
## systemparameter.set<sup>(deprecated)</sup>
Z
zengyawen 已提交
182

E
ester.zhou 已提交
183
set(key: string, value: string, callback: AsyncCallback&lt;void&gt;): void
E
esterzhou 已提交
184

E
ester.zhou 已提交
185
Sets a value for the system parameter with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
186

E
ester.zhou 已提交
187
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
188 189 190

**Parameters**

E
esterzhou 已提交
191 192
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
193 194
| key | string | Yes| Key of the system parameter.|
| value | string | Yes| Value of the system parameter to set.|
E
esterzhou 已提交
195
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
196

E
ester.zhou 已提交
197 198 199 200 201
> **NOTE**
>
> - This API can be used only for setting parameters of system applications.
> - SELinux and discretionary access control (DAC) rules must be configured for authorized system applications. For details, see [Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md).

Z
zengyawen 已提交
202 203
**Example**

P
update  
Peter_1988 已提交
204
```ts
Z
zengyawen 已提交
205
try {
E
ester.zhou 已提交
206
    systemparameter.set("test.parameter.key", "testValue", function (err, data) {
Z
zengyawen 已提交
207 208 209 210 211 212 213 214 215 216
    if (err == undefined) {
        console.log("set test.parameter.key value success :" + data)
    } else {
        console.log("set test.parameter.key value err:" + err.code)
    }});
}catch(e){
    console.log("set unexpected error: " + e);
}
```

E
ester.zhou 已提交
217
## systemparameter.set<sup>(deprecated)</sup>
Z
zengyawen 已提交
218

E
ester.zhou 已提交
219
set(key: string, value: string): Promise&lt;void&gt;
E
esterzhou 已提交
220

E
ester.zhou 已提交
221
Sets a value for the system parameter with the specified key. This API uses a promise to return the result.
E
esterzhou 已提交
222

E
ester.zhou 已提交
223
**System capability**: SystemCapability.Startup.SystemInfo
Z
zengyawen 已提交
224 225 226

**Parameters**

E
esterzhou 已提交
227 228
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
229 230
| key | string | Yes| Key of the system parameter.|
| value| string | Yes| Value of the system parameter to set.|
E
esterzhou 已提交
231 232 233 234 235

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
236
| Promise&lt;void&gt; | Promise used to return the execution result.|
Z
zengyawen 已提交
237

E
ester.zhou 已提交
238 239 240 241 242
> **NOTE**
>
> - This API can be used only for setting parameters of system applications.
> - SELinux and discretionary access control (DAC) rules must be configured for authorized system applications. For details, see [Service Management](../../../device-dev/subsystems/subsys-boot-init-sysparam.md).

Z
zengyawen 已提交
243 244
**Example**

P
update  
Peter_1988 已提交
245
```ts
Z
zengyawen 已提交
246
try {
E
ester.zhou 已提交
247
    var p = systemparameter.set("test.parameter.key", "testValue");
Z
zengyawen 已提交
248 249 250 251 252 253 254 255 256
    p.then(function (value) {
        console.log("set test.parameter.key success: " + value);
    }).catch(function (err) {
        console.log(" set test.parameter.key error: " + err.code);
    });
}catch(e){
    console.log("set unexpected error: " + e);
}
```