js-apis-power.md 10.4 KB
Newer Older
S
shawn_he 已提交
1
# @ohos.power (Power Manager)
Z
zengyawen 已提交
2

S
shawn_he 已提交
3
The **power** module provides APIs for rebooting and shutting down the system, as well as querying the screen status.
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6
> **NOTE**
>
S
shawn_he 已提交
7
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
S
shawn_he 已提交
8 9

## Modules to Import
Z
zengyawen 已提交
10

S
shawn_he 已提交
11
```js
Z
zengyawen 已提交
12 13 14
import power from '@ohos.power';
```

S
shawn_he 已提交
15
## power.shutdown
Z
zengyawen 已提交
16

S
shawn_he 已提交
17
shutdown(reason: string): void
Z
zengyawen 已提交
18 19 20

Shuts down the system.

S
shawn_he 已提交
21
**System API**: This is a system API.
S
shawn_he 已提交
22

S
shawn_he 已提交
23
**Required permission**: ohos.permission.REBOOT
Z
zengyawen 已提交
24

S
shawn_he 已提交
25 26
**System capability:** SystemCapability.PowerManager.PowerManager.Core

S
shawn_he 已提交
27 28
**Parameters**

S
shawn_he 已提交
29 30 31
| Name   | Type    | Mandatory  | Description   |
| ------ | ------ | ---- | ----- |
| reason | string | Yes   | Reason for system shutdown.|
S
shawn_he 已提交
32

S
shawn_he 已提交
33 34 35 36
**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
37
| ID  | Error Message   |
S
shawn_he 已提交
38 39 40
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

S
shawn_he 已提交
41 42
**Example**

S
shawn_he 已提交
43
```js
S
shawn_he 已提交
44 45 46 47 48
try {
    power.shutdown('shutdown_test');
} catch(err) {
    console.error('shutdown failed, err: ' + err);
}
S
shawn_he 已提交
49 50
```

S
shawn_he 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## power.reboot<sup>9+</sup>

reboot(reason: string): void

Reboots the system.

**System API**: This is a system API.

**Required permission**: ohos.permission.REBOOT

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Parameters**

| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| reason | string | Yes  | Reason for system reboot.|

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
73
| ID  | Error Message   |
S
shawn_he 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
try {
    power.reboot('reboot_test');
} catch(err) {
    console.error('reboot failed, err: ' + err);
}
```

## power.isActive<sup>9+</sup>

isActive(): boolean

S
shawn_he 已提交
91
Checks whether the current device is active. In the active state, the screen is on if the device has a screen and the device is not in sleep state if the device does not have a screen.
S
shawn_he 已提交
92 93 94 95 96 97 98

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
99
| ID  | Error Message   |
S
shawn_he 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
try {
    var isActive = power.isActive();
    console.info('power is active: ' + isActive);
} catch(err) {
    console.error('check active status failed, err: ' + err);
}
```

## power.wakeup<sup>9+</sup>

wakeup(detail: string): void

Wakes up a device.

**System API**: This is a system API.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Parameters**

| Name| Type  | Mandatory| Description      |
| ------ | ------ | ---- | ---------- |
| detail | string | Yes  | Reason for wakeup.|

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
134
| ID  | Error Message   |
S
shawn_he 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
try {
    power.wakeup('wakeup_test');
} catch(err) {
    console.error('wakeup failed, err: ' + err);
}
```

## power.suspend<sup>9+</sup>

suspend(): void

Hibernates a device.

**System API**: This is a system API.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
162
| ID  | Error Message   |
S
shawn_he 已提交
163 164 165 166 167 168 169 170 171 172 173 174
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
try {
    power.suspend();
} catch(err) {
    console.error('suspend failed, err: ' + err);
}
```
S
shawn_he 已提交
175

S
shawn_he 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
## power.getPowerMode<sup>9+</sup>

getPowerMode(): DevicePowerMode

Obtains the power mode of this device.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Return value**

| Type                                | Description      |
| ------------------------------------ | ---------- |
| [DevicePowerMode](#devicepowermode9) | Power mode.|

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
194
| ID  | Error Message   |
S
shawn_he 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
try {
    var mode = power.getPowerMode();
    console.info('power mode: ' + mode);
} catch(err) {
    console.error('get power mode failed, err: ' + err);
}
```

## power.setPowerMode<sup>9+</sup>

setPowerMode(mode: DevicePowerMode, callback: AsyncCallback&lt;void&gt;): void

Sets the power mode of this device. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permission**: ohos.permission.POWER_OPTIMIZATION

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Parameters**

| Name  | Type                                | Mandatory| Description                                                        |
| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| mode     | [DevicePowerMode](#devicepowermode9) | Yes  | Power mode.                                                  |
| callback | AsyncCallback&lt;void&gt;            | Yes  | Callback used to return the result. If the power mode is successfully set, **err** is **undefined**; otherwise, **err** is an error object.|

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
232
| ID  | Error Message   |
S
shawn_he 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE, err => {
    if (typeof err === 'undefined') {
        console.info('set power mode to MODE_PERFORMANCE');
    } else {
        console.error('set power mode failed, err: ' + err);
    }
});
```

## power.setPowerMode<sup>9+</sup>

setPowerMode(mode: DevicePowerMode): Promise&lt;void&gt;

Sets the power mode of this device. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permission**: ohos.permission.POWER_OPTIMIZATION

**System capability:** SystemCapability.PowerManager.PowerManager.Core

**Parameters**

| Name| Type                                | Mandatory| Description      |
| ------ | ------------------------------------ | ---- | ---------- |
| mode   | [DevicePowerMode](#devicepowermode9) | Yes  | Power mode.|

**Return value**

| Type               | Description                                  |
| ------------------- | -------------------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|

**Error codes**

For details about the error codes, see [Power Manager Error Codes](../errorcodes/errorcode-power.md).

S
shawn_he 已提交
276
| ID  | Error Message   |
S
shawn_he 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|---------|---------|
| 4900101 | Operation failed. Cannot connect to service.|

**Example**

```js
power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE)
.then(() => {
    console.info('set power mode to MODE_PERFORMANCE');
})
.catch(err => {
    console.error('set power mode failed, err: ' + err);
});
```

## power.rebootDevice<sup>(deprecated)</sup>
Z
zengyawen 已提交
293

S
shawn_he 已提交
294 295
rebootDevice(reason: string): void

S
shawn_he 已提交
296
> **NOTE**<br>This API is supported since API version 7 and is deprecated since API version 9. You are advised to use [power.reboot](#powerreboot9). The substitute API is available only for system applications.
S
shawn_he 已提交
297

S
shawn_he 已提交
298
Reboots the system.
S
shawn_he 已提交
299

S
shawn_he 已提交
300 301 302
**Required permission**: ohos.permission.REBOOT

**System capability:** SystemCapability.PowerManager.PowerManager.Core
S
shawn_he 已提交
303 304 305

**Parameters**

S
shawn_he 已提交
306 307 308
| Name   | Type    | Mandatory  | Description   |
| ------ | ------ | ---- | ----- |
| reason | string | Yes   | Reason for system reboot.|
S
shawn_he 已提交
309 310 311

**Example**

S
shawn_he 已提交
312
```js
S
shawn_he 已提交
313
power.rebootDevice('reboot_test');
S
shawn_he 已提交
314 315
```

S
shawn_he 已提交
316
## power.isScreenOn<sup>(deprecated)</sup>
S
shawn_he 已提交
317 318

isScreenOn(callback: AsyncCallback&lt;boolean&gt;): void
Z
zengyawen 已提交
319

S
shawn_he 已提交
320
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [power.isActive](#powerisactive9).
S
shawn_he 已提交
321 322 323 324

Checks the screen status of the current device. This API uses an asynchronous callback to return the result.

**System capability:** SystemCapability.PowerManager.PowerManager.Core
Z
zengyawen 已提交
325

S
shawn_he 已提交
326 327
**Parameters**

S
shawn_he 已提交
328 329
| Name  | Type                        | Mandatory| Description                                                        |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
330
| callback | AsyncCallback&lt;boolean&gt; | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the screen status obtained, where the value **true** indicates on and the value **false** indicates off. Otherwise, **err** is an error object.|
S
shawn_he 已提交
331 332 333

**Example**

S
shawn_he 已提交
334
```js
S
shawn_he 已提交
335 336 337
power.isScreenOn((err, data) => {
    if (typeof err === 'undefined') {
        console.info('screen on status is ' + data);
S
shawn_he 已提交
338
    } else {
S
shawn_he 已提交
339
        console.error('check screen status failed, err: ' + err);
S
shawn_he 已提交
340 341 342
    }
})
```
Z
zengyawen 已提交
343

S
shawn_he 已提交
344
## power.isScreenOn<sup>(deprecated)</sup>
S
shawn_he 已提交
345 346 347

isScreenOn(): Promise&lt;boolean&gt;

S
shawn_he 已提交
348
> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [power.isActive](#powerisactive9).
S
shawn_he 已提交
349 350 351 352

Checks the screen status of the current device. This API uses a promise to return the result.

**System capability:** SystemCapability.PowerManager.PowerManager.Core
S
shawn_he 已提交
353

S
shawn_he 已提交
354
**Return value**
S
shawn_he 已提交
355 356 357
| Type                  | Description                                              |
| ---------------------- | -------------------------------------------------- |
| Promise&lt;boolean&gt; | Promise used to return the result. The value **true** indicates that the screen is on, and the value **false** indicates the opposite.|
S
shawn_he 已提交
358 359 360

**Example**

S
shawn_he 已提交
361
```js
S
shawn_he 已提交
362
power.isScreenOn()
S
shawn_he 已提交
363 364
.then(data => {
    console.info('screen on status is ' + data);
S
shawn_he 已提交
365
})
S
shawn_he 已提交
366 367
.catch(err => {
    console.error('check screen status failed, err: ' + err);
S
shawn_he 已提交
368 369
})
```
S
shawn_he 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382

## DevicePowerMode<sup>9+</sup>

Enumerates power modes.

**System capability:** SystemCapability.PowerManager.PowerManager.Core

| Name                   | Value  | Description                  |
| ----------------------- | ---- | ---------------------- |
| MODE_NORMAL             | 600  | Standard mode. It is the default value.|
| MODE_POWER_SAVE         | 601  | Power saving mode.        |
| MODE_PERFORMANCE        | 602  | Performance mode.        |
| MODE_EXTREME_POWER_SAVE | 603  | Ultra power saving mode.    |