js-apis-power.md 2.8 KB
Newer Older
S
shawn_he 已提交
1
# Power Manager
Z
zengyawen 已提交
2

S
shawn_he 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
Z
zengyawen 已提交
5

S
shawn_he 已提交
6 7
The Power Manager module provides APIs for rebooting and shutting down the system, as well as querying the screen status.

S
shawn_he 已提交
8 9

## Modules to Import
Z
zengyawen 已提交
10 11 12 13 14

```
import power from '@ohos.power';
```

S
shawn_he 已提交
15
## System Capabilities
Z
zengyawen 已提交
16

S
shawn_he 已提交
17
SystemCapability.PowerManager.PowerManager.Core
Z
zengyawen 已提交
18 19


S
shawn_he 已提交
20
## power.shutdownDevice
Z
zengyawen 已提交
21

S
shawn_he 已提交
22
shutdownDevice(reason: string): void
Z
zengyawen 已提交
23 24 25

Shuts down the system.

S
shawn_he 已提交
26 27 28
**Required permission:** ohos.permission.SHUTDOWN

**Note:** This is a system API and it is used only for system applications.
Z
zengyawen 已提交
29

S
shawn_he 已提交
30 31
**Parameters**

S
shawn_he 已提交
32 33 34
| Name   | Type    | Mandatory  | Description   |
| ------ | ------ | ---- | ----- |
| reason | string | Yes   | Reason for system shutdown.|
S
shawn_he 已提交
35 36 37 38 39 40 41 42 43 44

**Example**

```
power.shutdownDevice("shutdown_test");
console.info('power_shutdown_device_test success')
```


## power.rebootDevice
Z
zengyawen 已提交
45

S
shawn_he 已提交
46 47 48 49
rebootDevice(reason: string): void

Restarts the device.

S
shawn_he 已提交
50
**Required permission:** ohos.permission.REBOOT (to reboot) or ohos.permission.REBOOT_UPDATER (to reboot and enter the updater mode)
S
shawn_he 已提交
51 52 53

**Parameters**

S
shawn_he 已提交
54 55 56
| Name   | Type    | Mandatory  | Description   |
| ------ | ------ | ---- | ----- |
| reason | string | Yes   | Reason for system reboot.|
S
shawn_he 已提交
57 58 59 60 61 62 63 64 65 66 67 68

**Example**

```
power.rebootDevice("reboot_test");
console.info('power_reboot_device_test success')
```


## power.isScreenOn

isScreenOn(callback: AsyncCallback<boolean>): void
Z
zengyawen 已提交
69 70 71

Checks the screen status of the current device.

S
shawn_he 已提交
72 73
**Parameters**

S
shawn_he 已提交
74 75 76
| Type      | Type                          | Mandatory  | Description                                      |
| -------- | ---------------------------- | ---- | ---------------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to obtain the return value.<br>Return value: The value **true** indicates that the screen is on, and the value **false** indicates the opposite.|
S
shawn_he 已提交
77 78 79 80 81 82

**Example**

```
power.isScreenOn((error, screenOn) => {
    if (typeof error === "undefined") {
Z
zengyawen 已提交
83
        console.info('screenOn status is ' + screenOn);
S
shawn_he 已提交
84
    } else {
Z
zengyawen 已提交
85
        console.log('error: ' + error);
S
shawn_he 已提交
86 87 88
    }
})
```
Z
zengyawen 已提交
89 90


S
shawn_he 已提交
91 92 93 94 95 96
## power.isScreenOn

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

Checks the screen status of the current device.

S
shawn_he 已提交
97 98 99
**Return Value**
| Type                    | Description                                     |
| ---------------------- | --------------------------------------- |
S
shawn_he 已提交
100
| Promise&lt;boolean&gt; | Promise used to obtain the return value. <br/>Return value: The value **true** indicates that the screen is on, and the value **false** indicates the opposite.|
S
shawn_he 已提交
101 102 103 104 105 106 107 108 109 110 111 112

**Example**

```
power.isScreenOn()
.then(screenOn => {
    console.info('screenOn status is ' + screenOn);
})
.catch(error => {
    console.log('error: ' + error);
})
```