1# @ohos.power (系统电源管理)(系统接口)
2
3该模块主要提供重启、关机、查询屏幕状态等接口。
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.power (系统电源管理)](js-apis-power.md)。
10
11## 导入模块
12
13```js
14import {power} from '@kit.BasicServicesKit';
15```
16
17## power.shutdown
18
19shutdown(reason: string): void
20
21系统关机。
22
23**系统接口:** 此接口为系统接口。
24
25**需要权限:** ohos.permission.REBOOT
26
27**系统能力:** SystemCapability.PowerManager.PowerManager.Core
28
29**参数:**
30
31| 参数名    | 类型     | 必填   | 说明    |
32| ------ | ------ | ---- | ----- |
33| reason | string | 是    | 关机原因;该参数必须为字符串类型。 |
34
35**错误码:**
36
37以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
38
39| 错误码ID   | 错误信息    |
40|---------|---------|
41| 4900101 | Failed to connect to the service. |
42| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
43| 201     | Permission verification failed. The application does not have the permission required to call the API. |
44| 202     | Permission verification failed. A non-system application calls a system API.  |
45
46
47**示例:**
48
49```js
50try {
51    power.shutdown('shutdown_test');
52} catch(err) {
53    console.error('shutdown failed, err: ' + err);
54}
55```
56
57## power.reboot<sup>9+</sup>
58
59reboot(reason: string): void
60
61重启设备。
62
63**系统接口:** 此接口为系统接口。
64
65**需要权限:** ohos.permission.REBOOT
66
67**系统能力:** SystemCapability.PowerManager.PowerManager.Core
68
69**参数:**
70
71| 参数名 | 类型   | 必填 | 说明       |
72| ------ | ------ | ---- | ---------- |
73| reason | string | 是   | 重启原因;该参数必须为字符串类型。 |
74
75**错误码:**
76
77以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
78
79| 错误码ID   | 错误信息    |
80|---------|---------|
81| 4900101 | Failed to connect to the service. |
82| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
83| 201     | Permission verification failed. The application does not have the permission required to call the API. |
84| 202     | Permission verification failed. A non-system application calls a system API.  |
85
86**示例:**
87
88```js
89try {
90    power.reboot('reboot_test');
91} catch(err) {
92    console.error('reboot failed, err: ' + err);
93}
94```
95
96## power.wakeup<sup>9+</sup>
97
98wakeup(detail: string): void
99
100唤醒设备。
101
102**系统接口:** 此接口为系统接口。
103
104**系统能力:** SystemCapability.PowerManager.PowerManager.Core
105
106**参数:**
107
108| 参数名 | 类型   | 必填 | 说明       |
109| ------ | ------ | ---- | ---------- |
110| detail | string | 是   | 唤醒原因;该参数必须为字符串类型。 |
111
112**错误码:**
113
114以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
115
116| 错误码ID   | 错误信息    |
117|---------|---------|
118| 4900101 | Failed to connect to the service. |
119| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
120| 202     | Permission verification failed. A non-system application calls a system API.  |
121
122**示例:**
123
124```js
125try {
126    power.wakeup('wakeup_test');
127} catch(err) {
128    console.error('wakeup failed, err: ' + err);
129}
130```
131
132## power.suspend<sup>9+</sup>
133
134suspend(isImmediate?: boolean): void
135
136休眠设备。
137
138**系统接口:** 此接口为系统接口。
139
140**系统能力:** SystemCapability.PowerManager.PowerManager.Core
141
142**参数:**
143
144| 参数名 | 类型   | 必填 | 说明       |
145| ------ | ------ | ---- | ---------- |
146| isImmediate<sup>10+</sup> | boolean |  否  | 是否直接休眠设备。不填该参数则默认为false由系统自动检测何时进入休眠。<br>**说明:** 从API version 10开始,支持该参数。|
147
148
149**错误码:**
150
151以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
152
153| 错误码ID   | 错误信息    |
154|---------|---------|
155| 4900101 | Failed to connect to the service. |
156| 202     | Permission verification failed. A non-system application calls a system API.  |
157| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
158
159**示例:**
160
161```js
162try {
163    power.suspend();
164} catch(err) {
165    console.error('suspend failed, err: ' + err);
166}
167```
168
169## power.setPowerMode<sup>9+</sup>
170
171setPowerMode(mode: DevicePowerMode, callback: AsyncCallback&lt;void&gt;): void
172
173设置当前设备的电源模式。使用callback异步回调。
174
175**系统接口:** 此接口为系统接口。
176
177**需要权限:** ohos.permission.POWER_OPTIMIZATION
178
179**系统能力:** SystemCapability.PowerManager.PowerManager.Core
180
181**参数:**
182
183| 参数名   | 类型                                 | 必填 | 说明                                                         |
184| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
185| mode     | DevicePowerMode | 是   | 电源模式;该参数类型是一个枚举类。                                                   |
186| callback | AsyncCallback&lt;void&gt;            | 是   | 回调函数。当设置电源模式成功,err为undefined,否则为错误对象。 |
187
188**错误码:**
189
190以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
191
192| 错误码ID   | 错误信息    |
193|---------|---------|
194| 4900101 | Failed to connect to the service. |
195| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
196| 201     | Permission verification failed. The application does not have the permission required to call the API. |
197| 202     | Permission verification failed. A non-system application calls a system API.  |
198
199**示例:**
200
201```js
202power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE, (err: Error) => {
203    if (typeof err === 'undefined') {
204        console.info('set power mode to MODE_PERFORMANCE');
205    } else {
206        console.error('set power mode failed, err: ' + err);
207    }
208});
209```
210
211## power.setPowerMode<sup>9+</sup>
212
213setPowerMode(mode: DevicePowerMode): Promise&lt;void&gt;
214
215设置当前设备的电源模式。使用Promise异步回调。
216
217**系统接口:** 此接口为系统接口。
218
219**需要权限:** ohos.permission.POWER_OPTIMIZATION
220
221**系统能力:** SystemCapability.PowerManager.PowerManager.Core
222
223**参数:**
224
225| 参数名 | 类型                                 | 必填 | 说明       |
226| ------ | ------------------------------------ | ---- | ---------- |
227| mode   | DevicePowerMode | 是   | 电源模式;该参数类型是一个枚举类。 |
228
229**返回值:**
230
231| 类型                | 说明                                   |
232| ------------------- | -------------------------------------- |
233| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
234
235**错误码:**
236
237以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
238
239| 错误码ID   | 错误信息    |
240|---------|---------|
241| 4900101 | Failed to connect to the service. |
242| 401     | Parameter error. Possible causes: 1.Parameter verification failed. |
243| 201     | Permission verification failed. The application does not have the permission required to call the API. |
244| 202     | Permission verification failed. A non-system application calls a system API.  |
245
246**示例:**
247
248```js
249power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE)
250.then(() => {
251    console.info('set power mode to MODE_PERFORMANCE');
252})
253.catch((err : Error)=> {
254    console.error('set power mode failed, err: ' + err);
255});
256```
257
258## power.setScreenOffTime<sup>12+</sup>
259
260setScreenOffTime(timeout: number): void
261
262设置熄屏超时时间。
263
264**系统接口:** 此接口为系统接口。
265
266**系统能力:** SystemCapability.PowerManager.PowerManager.Core
267
268**参数:**
269
270| 参数名    | 类型     | 必填   | 说明    |
271| ------ | ------ | ---- | ----- |
272| timeout | number | 是    | 熄屏超时时间,单位是毫秒,大于0代表熄屏超时时间,-1代表恢复默认超时时间,其它是无效值。 |
273
274**错误码:**
275
276以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
277
278| 错误码ID   | 错误信息    |
279|---------|---------|
280| 4900101 | Failed to connect to the service. |
281| 401     | Parameter error. Possible causes: 1. Parameter verification failed. |
282| 202     | Permission verification failed. A non-system application calls a system API.  |
283
284**示例:**
285
286```js
287try {
288    power.setScreenOffTime(30000);
289} catch(err) {
290    console.error('set screen off time failed, err: ' + err);
291}
292```
293
294## power.hibernate<sup>12+</sup>
295
296hibernate(clearMemory: boolean): void
297
298休眠设备。
299
300**系统接口:** 此接口为系统接口。
301
302**系统能力:** SystemCapability.PowerManager.PowerManager.Core
303
304**参数:**
305
306| 参数名    | 类型     | 必填   | 说明    |
307| ------ | ------ | ---- | ----- |
308| clearMemory | boolean | 是    | true 代表在进入休眠之前清理内存,否则为false。 |
309
310**错误码:**
311
312以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
313
314| 错误码ID   | 错误信息    |
315|---------|---------|
316| 4900101 | Failed to connect to the service. |
317| 202     | Permission verification failed. A non-system application calls a system API.  |
318| 401     | Parameter error. Possible causes: 1.Incorrect parameter types. |
319
320**示例:**
321
322```js
323try {
324    power.hibernate(true);
325} catch(err) {
326    console.error('hibernate failed, err: ' + err);
327}
328```