1# @ohos.power (系统电源管理)
2
3该模块主要提供重启、关机、查询屏幕状态等接口。
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```js
12import {power} from '@kit.BasicServicesKit';
13```
14
15## power.isActive<sup>9+</sup>
16
17isActive(): boolean
18
19检测当前设备是否处于活动状态。有屏的设备为亮屏状态,无屏的设备为非休眠状态。
20
21**系统能力:** SystemCapability.PowerManager.PowerManager.Core
22
23**错误码:**
24
25以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
26
27| 错误码ID   | 错误信息    |
28|---------|---------|
29| 4900101 | Failed to connect to the service. |
30
31**示例:**
32
33```js
34try {
35    let isActive = power.isActive();
36    console.info('power is active: ' + isActive);
37} catch(err) {
38    console.error('check active status failed, err: ' + err);
39}
40```
41
42## power.rebootDevice<sup>(deprecated)</sup>
43
44rebootDevice(reason: string): void
45
46> **说明:**<br>从API version 7开始支持,从API version 9开始不再维护。替代接口能力仅对系统应用开放。
47
48重启设备。
49
50**需要权限:** ohos.permission.REBOOT,该权限仅系统应用可申请。
51
52**系统能力:** SystemCapability.PowerManager.PowerManager.Core
53
54
55**参数:**
56
57| 参数名    | 类型     | 必填   | 说明    |
58| ------ | ------ | ---- | ----- |
59| reason | string | 是    | 重启原因。 |
60
61**示例:**
62
63```js
64power.rebootDevice('reboot_test');
65```
66
67## power.getPowerMode<sup>9+</sup>
68
69getPowerMode(): DevicePowerMode
70
71获取当前设备的电源模式。
72
73**系统能力:** SystemCapability.PowerManager.PowerManager.Core
74
75**返回值:**
76
77| 类型                                 | 说明       |
78| ------------------------------------ | ---------- |
79| [DevicePowerMode](#devicepowermode9) | 电源模式。 |
80
81**错误码:**
82
83以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
84
85| 错误码ID   | 错误信息    |
86|---------|---------|
87| 4900101 | Failed to connect to the service. |
88
89**示例:**
90
91```js
92try {
93    let mode = power.getPowerMode();
94    console.info('power mode: ' + mode);
95} catch(err) {
96    console.error('get power mode failed, err: ' + err);
97}
98```
99
100## power.isStandby<sup>10+</sup>
101
102isStandby(): boolean
103
104检测当前设备是否进入待机低功耗续航模式。
105
106**系统能力:** SystemCapability.PowerManager.PowerManager.Core
107
108**返回值:**
109
110| 类型                | 说明                                   |
111| ------------------- | -------------------------------------- |
112| boolean | 进入待机模式返回true,否则返回false。 |
113
114**错误码:**
115
116以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
117
118| 错误码ID   | 错误信息    |
119|---------|---------|
120| 4900101 | Failed to connect to the service. |
121
122**示例:**
123
124```js
125try {
126    let isStandby = power.isStandby();
127    console.info('device is in standby: ' + isStandby);
128} catch(err) {
129    console.error('check isStandby failed, err: ' + err);
130}
131```
132
133## power.isScreenOn<sup>(deprecated)</sup>
134
135isScreenOn(callback: AsyncCallback&lt;boolean&gt;): void
136
137> **说明:**<br>从API version 9开始不再维护,建议使用[power.isActive](#powerisactive9)替代。
138
139检测当前设备的亮灭屏状态。使用callback异步回调。
140
141**系统能力:** SystemCapability.PowerManager.PowerManager.Core
142
143**参数:**
144
145| 参数名   | 类型                         | 必填 | 说明                                                         |
146| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
147| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。当检测成功,err为undefined,data为获取到的亮灭屏状态,返回true表示亮屏,返回false表示灭屏;否则为错误对象。 |
148
149**示例:**
150
151```js
152power.isScreenOn((err: Error, data: boolean) => {
153    if (typeof err === 'undefined') {
154        console.info('screen on status is ' + data);
155    } else {
156        console.error('check screen status failed, err: ' + err);
157    }
158})
159```
160
161## power.isScreenOn<sup>(deprecated)</sup>
162
163isScreenOn(): Promise&lt;boolean&gt;
164
165> **说明:**<br>从API version 9开始不再维护,建议使用[power.isActive](#powerisactive9)替代。
166
167检测当前设备的亮灭屏状态。使用Promise异步回调。
168
169**系统能力:** SystemCapability.PowerManager.PowerManager.Core
170
171**返回值:**
172| 类型                   | 说明                                               |
173| ---------------------- | -------------------------------------------------- |
174| Promise&lt;boolean&gt; | Promise对象。返回true表示亮屏;返回false表示灭屏。 |
175
176**示例:**
177
178```js
179power.isScreenOn()
180.then((data: boolean) => {
181    console.info('screen on status is ' + data);
182})
183.catch((err: Error) => {
184    console.error('check screen status failed, err: ' + err);
185})
186```
187
188## DevicePowerMode<sup>9+</sup>
189
190表示电源模式的枚举值。
191
192**系统能力:** SystemCapability.PowerManager.PowerManager.Core
193
194| 名称                    | 值   | 说明                   |
195| ----------------------- | ---- | ---------------------- |
196| MODE_NORMAL             | 600  | 表示标准模式,默认值。 |
197| MODE_POWER_SAVE         | 601  | 表示省电模式。         |
198| MODE_PERFORMANCE        | 602  | 表示性能模式。         |
199| MODE_EXTREME_POWER_SAVE | 603  | 表示超级省电模式。     |
200