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

E
esterzhou 已提交
3 4 5 6 7
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> - 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 9 10 11 12 13

```
import parameter from '@ohos.systemParameter'
```


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

E
esterzhou 已提交
16
getSync(key: string, def?: 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 27 28 29 30 31 32 33
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| def | string | No| Default Value|

**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 37 38 39 40 41 42 43 44 45 46

**Example**

```
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
esterzhou 已提交
49 50 51 52 53
get(key: string, callback: AsyncCallback<string>)

Obtains the value of the attribute with the specified key. This API uses an asynchronous callback to return the result.

**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<string> | Yes| Callback used to return the result.|
Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

**Example**

```
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 80
## parameter.get

get(key: string, def: string, callback: AsyncCallback<string>)
Z
zengyawen 已提交
81

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

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

**Parameters**

E
esterzhou 已提交
88 89 90 91 92
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| def | string | Yes| Default Value|
| callback | AsyncCallback<string> | Yes| Callback used to return the result.|
Z
zengyawen 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

**Example**

```
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 113 114 115
## parameter.get

get(key: string, def?: string)

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 124 125 126 127 128 129 130
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| def | string | No| Default Value|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise<string> | Promise used to return the execution result.|
Z
zengyawen 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

**Example**

```
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 150
## parameter.setSync

setSync(key: string, value: string)
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 165 166 167 168 169 170 171 172 173

**Example**

```
try {
    parameter.setSync("test.parameter.key", "default");
}catch(e){
    console.log("set unexpected error: " + e);
}
```


E
esterzhou 已提交
174
## parameter.set(key: string, value: string, callback: AsyncCallback<void>)
Z
zengyawen 已提交
175

E
esterzhou 已提交
176 177 178 179 180
set(key: string, value: string, callback: AsyncCallback<void>)

Sets a value for the attribute with the specified key. This API uses an asynchronous callback to return the result.

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

**Parameters**

E
esterzhou 已提交
184 185 186 187 188
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| def | string | Yes| Default Value|
| callback | AsyncCallback<void> | Yes| Callback used to return the result.|
Z
zengyawen 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

**Example**

```
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
esterzhou 已提交
206
## parameter.set(key: string, def?: string)
Z
zengyawen 已提交
207

E
esterzhou 已提交
208 209 210 211 212
set(key: string, def?: string)

Sets a value for the attribute with the specified key. This API uses a promise to return the result.

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

**Parameters**

E
esterzhou 已提交
216 217 218 219 220 221 222 223 224 225
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the system attribute.|
| def | string | No| Default Value|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise<string> | Promise used to return the execution result.|
Z
zengyawen 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

**Example**

```
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);
}
```