• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '@ohos.power';
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 | If connecting to the service failed. |
42
43**示例:**
44
45```js
46try {
47    power.shutdown('shutdown_test');
48} catch(err) {
49    console.error('shutdown failed, err: ' + err);
50}
51```
52
53## power.reboot<sup>9+</sup>
54
55reboot(reason: string): void
56
57重启设备。
58
59**系统接口:** 此接口为系统接口。
60
61**需要权限:** ohos.permission.REBOOT
62
63**系统能力:** SystemCapability.PowerManager.PowerManager.Core
64
65**参数:**
66
67| 参数名 | 类型   | 必填 | 说明       |
68| ------ | ------ | ---- | ---------- |
69| reason | string | 是   | 重启原因。 |
70
71**错误码:**
72
73以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
74
75| 错误码ID   | 错误信息    |
76|---------|---------|
77| 4900101 | If connecting to the service failed. |
78
79**示例:**
80
81```js
82try {
83    power.reboot('reboot_test');
84} catch(err) {
85    console.error('reboot failed, err: ' + err);
86}
87```
88
89## power.wakeup<sup>9+</sup>
90
91wakeup(detail: string): void
92
93唤醒设备。
94
95**系统接口:** 此接口为系统接口。
96
97**系统能力:** SystemCapability.PowerManager.PowerManager.Core
98
99**参数:**
100
101| 参数名 | 类型   | 必填 | 说明       |
102| ------ | ------ | ---- | ---------- |
103| detail | string | 是   | 唤醒原因。 |
104
105**错误码:**
106
107以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
108
109| 错误码ID   | 错误信息    |
110|---------|---------|
111| 4900101 | If connecting to the service failed. |
112
113**示例:**
114
115```js
116try {
117    power.wakeup('wakeup_test');
118} catch(err) {
119    console.error('wakeup failed, err: ' + err);
120}
121```
122
123## power.suspend<sup>9+</sup>
124
125suspend(isImmediate?: boolean): void
126
127休眠设备。
128
129**系统接口:** 此接口为系统接口。
130
131**系统能力:** SystemCapability.PowerManager.PowerManager.Core
132
133**参数:**
134
135| 参数名 | 类型   | 必填 | 说明       |
136| ------ | ------ | ---- | ---------- |
137| isImmediate<sup>10+</sup> | boolean |  否  | 是否直接休眠设备。不填该参数则默认为false由系统自动检测何时进入休眠。<br>**说明:** 从API version 10开始,支持该参数。|
138
139
140**错误码:**
141
142以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
143
144| 错误码ID   | 错误信息    |
145|---------|---------|
146| 4900101 | If connecting to the service failed. |
147
148**示例:**
149
150```js
151try {
152    power.suspend();
153} catch(err) {
154    console.error('suspend failed, err: ' + err);
155}
156```
157
158## power.setPowerMode<sup>9+</sup>
159
160setPowerMode(mode: DevicePowerMode, callback: AsyncCallback&lt;void&gt;): void
161
162设置当前设备的电源模式。使用callback异步回调。
163
164**系统接口:** 此接口为系统接口。
165
166**需要权限:** ohos.permission.POWER_OPTIMIZATION
167
168**系统能力:** SystemCapability.PowerManager.PowerManager.Core
169
170**参数:**
171
172| 参数名   | 类型                                 | 必填 | 说明                                                         |
173| -------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
174| mode     | DevicePowerMode | 是   | 电源模式。                                                   |
175| callback | AsyncCallback&lt;void&gt;            | 是   | 回调函数。当设置电源模式成功,err为undefined,否则为错误对象。 |
176
177**错误码:**
178
179以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
180
181| 错误码ID   | 错误信息    |
182|---------|---------|
183| 4900101 | If connecting to the service failed. |
184
185**示例:**
186
187```js
188power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE, (err: Error) => {
189    if (typeof err === 'undefined') {
190        console.info('set power mode to MODE_PERFORMANCE');
191    } else {
192        console.error('set power mode failed, err: ' + err);
193    }
194});
195```
196
197## power.setPowerMode<sup>9+</sup>
198
199setPowerMode(mode: DevicePowerMode): Promise&lt;void&gt;
200
201设置当前设备的电源模式。使用Promise异步回调。
202
203**系统接口:** 此接口为系统接口。
204
205**需要权限:** ohos.permission.POWER_OPTIMIZATION
206
207**系统能力:** SystemCapability.PowerManager.PowerManager.Core
208
209**参数:**
210
211| 参数名 | 类型                                 | 必填 | 说明       |
212| ------ | ------------------------------------ | ---- | ---------- |
213| mode   | DevicePowerMode | 是   | 电源模式。 |
214
215**返回值:**
216
217| 类型                | 说明                                   |
218| ------------------- | -------------------------------------- |
219| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
220
221**错误码:**
222
223以下错误码的详细介绍请参见[系统电源管理错误码](errorcode-power.md)。
224
225| 错误码ID   | 错误信息    |
226|---------|---------|
227| 4900101 | If connecting to the service failed. |
228
229**示例:**
230
231```js
232power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE)
233.then(() => {
234    console.info('set power mode to MODE_PERFORMANCE');
235})
236.catch((err : Error)=> {
237    console.error('set power mode failed, err: ' + err);
238});
239```
240