harmony 鸿蒙@ohos.power (系统电源管理)(系统接口)
@ohos.power (系统电源管理)(系统接口)
该模块主要提供重启、关机、查询屏幕状态等接口。
说明:
本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
当前页面仅包含本模块的系统接口,其他公开接口参见@ohos.power (系统电源管理)。
导入模块
import {power} from '@kit.BasicServicesKit';
power.shutdown
shutdown(reason: string): void
系统关机。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.REBOOT
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
reason | string | 是 | 关机原因;该参数必须为字符串类型。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
try {
power.shutdown('shutdown_test');
} catch(err) {
console.error('shutdown failed, err: ' + err);
}
power.reboot9+
reboot(reason: string): void
重启设备。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.REBOOT
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
reason | string | 是 | 重启原因;该参数必须为字符串类型。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
try {
power.reboot('reboot_test');
} catch(err) {
console.error('reboot failed, err: ' + err);
}
power.wakeup9+
wakeup(detail: string): void
唤醒设备。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_MANAGER
API version 9-18,使用该接口无需权限;从API version 19开始,需要申请“ohos.permission.POWER_MANAGER”权限。
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
detail | string | 是 | 唤醒原因;该参数必须为字符串类型。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
try {
power.wakeup('wakeup_test');
} catch(err) {
console.error('wakeup failed, err: ' + err);
}
power.suspend9+
suspend(isImmediate?: boolean): void
休眠设备。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_MANAGER
API version 9-18,使用该接口无需权限;从API version 19开始,需要申请“ohos.permission.POWER_MANAGER”权限。
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
isImmediate10+ | boolean | 否 | 是否直接休眠设备。true表示灭屏后立即进入休眠,不填该参数则默认为false,表示灭屏后由系统自动检测何时进入休眠。如果只想做灭屏操作,建议不填参数。 说明: 从API version 10开始,支持该参数。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
示例:
try {
power.suspend();
} catch(err) {
console.error('suspend failed, err: ' + err);
}
power.setPowerMode9+
setPowerMode(mode: DevicePowerMode, callback: AsyncCallback<void>): void
设置当前设备的电源模式。使用callback异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_OPTIMIZATION
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
mode | DevicePowerMode | 是 | 电源模式;该参数类型是一个枚举类。 |
callback | AsyncCallback<void> | 是 | 回调函数。当设置电源模式成功,err为undefined,否则为错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1.Parameter verification failed. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE, (err: Error) => {
if (typeof err === 'undefined') {
console.info('set power mode to MODE_PERFORMANCE');
} else {
console.error('set power mode failed, err: ' + err);
}
});
power.setPowerMode9+
setPowerMode(mode: DevicePowerMode): Promise<void>
设置当前设备的电源模式。使用Promise异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_OPTIMIZATION
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
mode | DevicePowerMode | 是 | 电源模式;该参数类型是一个枚举类。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | Promise对象。无返回结果的Promise对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1.Parameter verification failed. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE)
.then(() => {
console.info('set power mode to MODE_PERFORMANCE');
})
.catch((err : Error)=> {
console.error('set power mode failed, err: ' + err);
});
power.setScreenOffTime12+
setScreenOffTime(timeout: number): void
设置熄屏超时时间。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_MANAGER
API version 12-18,使用该接口无需权限;从API version 19开始,需要申请“ohos.permission.POWER_MANAGER”权限。
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
timeout | number | 是 | 熄屏超时时间,单位是毫秒,大于0代表熄屏超时时间,-1代表恢复默认超时时间,其它是无效值。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
401 | Parameter error. Possible causes: 1. Parameter verification failed. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
示例:
try {
power.setScreenOffTime(30000);
} catch(err) {
console.error('set screen off time failed, err: ' + err);
}
power.hibernate12+
hibernate(clearMemory: boolean): void
休眠设备。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.POWER_MANAGER
API version 12-18,使用该接口无需权限;从API version 19开始,需要申请“ohos.permission.POWER_MANAGER”权限。
系统能力: SystemCapability.PowerManager.PowerManager.Core
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
clearMemory | boolean | 是 | true 代表在进入休眠之前清理内存,否则为false。 |
错误码:
错误码ID | 错误信息 |
---|---|
4900101 | Failed to connect to the service. |
201 | Permission verification failed. The application does not have the permission required to call the API. |
202 | Permission verification failed. A non-system application calls a system API. |
401 | Parameter error. Possible causes: 1.Incorrect parameter types. |
示例:
try {
power.hibernate(true);
} catch(err) {
console.error('hibernate failed, err: ' + err);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Basic Services Kit(基础服务)
harmony 鸿蒙Print_PrintAttributes
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦