js-apis-system-parameter.md 6.0 KB
Newer Older
E
esterzhou 已提交
1
# System Parameter
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
> **NOTE**<br>
E
esterzhou 已提交
4 5 6 7
> - 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.
> - This is a system API and cannot be called by third-party applications.

## Modules to Import
Z
zengyawen 已提交
8

P
update  
Peter_1988 已提交
9
```ts
Z
zengyawen 已提交
10 11 12 13
import parameter from '@ohos.systemParameter'
```


E
esterzhou 已提交
14
## parameter.getSync
Z
zengyawen 已提交
15

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

E
esterzhou 已提交
18
Obtains the value of the attribute with the specified key.
Z
zengyawen 已提交
19

E
esterzhou 已提交
20
**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
21 22 23

**Parameters**

E
esterzhou 已提交
24 25 26
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
E
ester.zhou 已提交
27
| def | string | No| Default value.|
E
esterzhou 已提交
28 29 30 31 32 33

**Return value**

| Type| Description|
| -------- | -------- |
| string | System attribute value. 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 已提交
34 35 36

**Example**

P
update  
Peter_1988 已提交
37
```ts
Z
zengyawen 已提交
38 39 40 41 42 43 44 45 46
try {
    var info = parameter.getSync("test.parameter.key");
    console.log(JSON.stringify(info));
}catch(e){
    console.log("getSync unexpected error: " + e);
}
```


E
esterzhou 已提交
47
## parameter.get
Z
zengyawen 已提交
48

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

E
ester.zhou 已提交
51
Obtains the value of the attribute with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
52 53

**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
54 55 56

**Parameters**

E
esterzhou 已提交
57 58 59 60
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
61 62 63

**Example**

P
update  
Peter_1988 已提交
64
```ts
Z
zengyawen 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
try {
    parameter.get("test.parameter.key", function (err, data) {
    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
esterzhou 已提交
78 79
## parameter.get

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

E
ester.zhou 已提交
82
Obtains the value of the attribute with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
83 84

**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
85 86 87

**Parameters**

E
esterzhou 已提交
88 89 90
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
E
ester.zhou 已提交
91
| def | string | Yes| Default value.|
E
esterzhou 已提交
92
| callback | AsyncCallback&lt;string&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
93 94 95

**Example**

P
update  
Peter_1988 已提交
96
```ts
Z
zengyawen 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
try {
    parameter.get("test.parameter.key", "default", function (err, data) {
        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
esterzhou 已提交
111 112
## parameter.get

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

E
ester.zhou 已提交
115
Obtains the value of the attribute with the specified key. This API uses a promise to return the result.
Z
zengyawen 已提交
116

E
esterzhou 已提交
117
**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
118 119 120

**Parameters**

E
esterzhou 已提交
121 122 123
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
E
ester.zhou 已提交
124
| def | string | No| Default value.|
E
esterzhou 已提交
125 126 127 128 129 130

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;string&gt; | Promise used to return the execution result.|
Z
zengyawen 已提交
131 132 133

**Example**

P
update  
Peter_1988 已提交
134
```ts
Z
zengyawen 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
try {
    var p = parameter.get("test.parameter.key");
    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
esterzhou 已提交
148 149
## parameter.setSync

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

Sets a value for the attribute with the specified key.

E
esterzhou 已提交
154 155
**System capability**: SystemCapability.Startup.SysInfo

Z
zengyawen 已提交
156 157
**Parameters**

E
esterzhou 已提交
158 159 160 161
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| value | string | Yes| System attribute value to set.|
Z
zengyawen 已提交
162 163 164

**Example**

P
update  
Peter_1988 已提交
165
```ts
Z
zengyawen 已提交
166 167 168 169 170 171 172 173
try {
    parameter.setSync("test.parameter.key", "default");
}catch(e){
    console.log("set unexpected error: " + e);
}
```


E
ester.zhou 已提交
174
## parameter.set
Z
zengyawen 已提交
175

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

E
ester.zhou 已提交
178
Sets a value for the attribute with the specified key. This API uses an asynchronous callback to return the result.
E
esterzhou 已提交
179 180

**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
181 182 183

**Parameters**

E
esterzhou 已提交
184 185 186
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
E
ester.zhou 已提交
187
| value | string | Yes| System attribute value to set.|
E
esterzhou 已提交
188
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result.|
Z
zengyawen 已提交
189 190 191

**Example**

P
update  
Peter_1988 已提交
192
```ts
Z
zengyawen 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
try {
    parameter.set("test.parameter.key", "testValue", function (err, data) {
    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 已提交
206
## parameter.set
Z
zengyawen 已提交
207

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

E
ester.zhou 已提交
210
Sets a value for the attribute with the specified key. This API uses a promise to return the result.
E
esterzhou 已提交
211 212

**System capability**: SystemCapability.Startup.SysInfo
Z
zengyawen 已提交
213 214 215

**Parameters**

E
esterzhou 已提交
216 217 218
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
E
ester.zhou 已提交
219
| value| string | Yes | System attribute value to set.|
E
esterzhou 已提交
220 221 222 223 224

**Return value**

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

**Example**

P
update  
Peter_1988 已提交
229
```ts
Z
zengyawen 已提交
230 231 232 233 234 235 236 237 238 239 240
try {
    var p = para.set("test.parameter.key", "testValue");
    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);
}
```