• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.power (Power Management)
2
3<!--Kit: Basic Services Kit-->
4<!--Subsystem: PowerManager-->
5<!--Owner: @zhang-yinglie; @volcano_wang-->
6<!--Designer: @wangyantian0-->
7<!--Tester: @alien0208-->
8<!--Adviser: @w_Machine_cc-->
9
10The **power** module provides APIs for rebooting and shutting down the system, as well as querying the screen status. You can use these APIs to obtain the device activity status, power mode, and screen on/off status.
11> **NOTE**
12>
13> 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.
14
15## Modules to Import
16
17```js
18import {power} from '@kit.BasicServicesKit';
19```
20
21## power.isActive<sup>9+</sup>
22
23isActive(): boolean
24
25Checks whether the current device is active.
26- A device with a screen is active when the screen is on and inactive when the screen is off.
27- A device without a screen is active when it exits the sleep mode and inactive when it enters the sleep mode.
28
29**System capability**: SystemCapability.PowerManager.PowerManager.Core
30
31**Returns**
32
33| Type               | Description                                  |
34| ------------------- | -------------------------------------- |
35| boolean | Returns **true** if the device is active; returns **false** otherwise.|
36
37**Example**
38
39```js
40let isActive = power.isActive();
41console.info('power is active: ' + isActive);
42```
43
44## power.rebootDevice<sup>(deprecated)</sup>
45
46rebootDevice(reason: string): void
47
48> **NOTE**<br>This API is supported since API version 7 and is deprecated since API version 9. The substitute API is available only for system applications.
49
50Reboots a device.
51
52**Required permissions**: ohos.permission.REBOOT (available only for system applications)
53
54**System capability**: SystemCapability.PowerManager.PowerManager.Core
55
56
57**Parameters**
58
59| Name   | Type    | Mandatory  | Description   |
60| ------ | ------ | ---- | ----- |
61| reason | string | Yes   | Reason for system reboot.|
62
63**Example**
64
65```js
66power.rebootDevice('reboot_test');
67```
68
69## power.getPowerMode<sup>9+</sup>
70
71getPowerMode(): DevicePowerMode
72
73Obtains the power mode of this device.
74
75**System capability**: SystemCapability.PowerManager.PowerManager.Core
76
77**Returns**
78
79| Type                                | Description      |
80| ------------------------------------ | ---------- |
81| [DevicePowerMode](#devicepowermode9) | Power mode.|
82
83**Example**
84
85```js
86let mode = power.getPowerMode();
87console.info('power mode: ' + mode);
88```
89
90## power.isStandby<sup>10+</sup>
91
92isStandby(): boolean
93
94Checks whether the device is in standby mode.
95
96**System capability**: SystemCapability.PowerManager.PowerManager.Core
97
98**Returns**
99
100| Type               | Description                                  |
101| ------------------- | -------------------------------------- |
102| boolean | The value **true** indicates that the device is in standby mode, and the value **false** indicates the opposite.|
103
104**Error codes**
105
106For details about the error codes, see [Power Manager Error Codes](errorcode-power.md).
107
108| ID  | Error Message   |
109|---------|---------|
110| 4900101 | Failed to connect to the service. |
111
112**Example**
113
114```js
115try {
116    let isStandby = power.isStandby();
117    console.info('device is in standby: ' + isStandby);
118} catch(err) {
119    console.error('check isStandby failed, err: ' + err);
120}
121```
122
123## power.isScreenOn<sup>(deprecated)</sup>
124
125isScreenOn(callback: AsyncCallback&lt;boolean&gt;): void
126
127> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [power.isActive](#powerisactive9) instead.
128
129Checks the screen status of the current device. This API uses an asynchronous callback to return the result.
130
131**System capability**: SystemCapability.PowerManager.PowerManager.Core
132
133**Parameters**
134
135| Name  | Type                        | Mandatory| Description                                                        |
136| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
137| 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.|
138
139**Example**
140
141```js
142power.isScreenOn((err: Error, data: boolean) => {
143    if (typeof err === 'undefined') {
144        console.info('screen on status is ' + data);
145    } else {
146        console.error('check screen status failed, err: ' + err);
147    }
148})
149```
150
151## power.isScreenOn<sup>(deprecated)</sup>
152
153isScreenOn(): Promise&lt;boolean&gt;
154
155> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [power.isActive](#powerisactive9) instead.
156
157Checks the screen status of the current device. This API uses a promise to return the result.
158
159**System capability**: SystemCapability.PowerManager.PowerManager.Core
160
161**Returns**
162| Type                  | Description                                              |
163| ---------------------- | -------------------------------------------------- |
164| 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.|
165
166**Example**
167
168```js
169power.isScreenOn()
170.then((data: boolean) => {
171    console.info('screen on status is ' + data);
172})
173.catch((err: Error) => {
174    console.error('check screen status failed, err: ' + err);
175})
176```
177
178## DevicePowerMode<sup>9+</sup>
179
180Enumerates power modes.
181
182**System capability**: SystemCapability.PowerManager.PowerManager.Core
183
184| Name                   | Value  | Description                  |
185| ----------------------- | ---- | ---------------------- |
186| MODE_NORMAL             | 600  | Standard mode. It is the default value.|
187| MODE_POWER_SAVE         | 601  | Power saving mode.        |
188| MODE_PERFORMANCE        | 602  | Performance mode.        |
189| MODE_EXTREME_POWER_SAVE | 603  | Ultra power saving mode.    |
190| MODE_CUSTOM_POWER_SAVE<sup>20+</sup>  | 650  | Custom power saving mode.    |
191