OpenHarmony开发者论坛
标题:
使用power系统级的挂起和唤醒api后,应用的功能失效的原因?
[打印本页]
作者:
luocl
时间:
2025-2-26 16:22
标题:
使用power系统级的挂起和唤醒api后,应用的功能失效的原因?
【问题描述】
问题现象和发生的背景
想通过power系统级的api去使得设备休眠,但是唤醒之后应用的功能会失效。
相关的代码,截图,日志信息
代码:
import power from '@ohos.power'
import inputMonitor from '@ohos.multimodalInput.inputMonitor'
import { TouchEvent } from '@ohos.multimodalInput.touchEvent'
@Component
export default struct ScreenSleep {
@State @Watch('watchIsActive') isActive: boolean = true //当前设备活动状态标识
@State touchCount: number = 0 //记录当屏幕休眠状态下,用户接触屏幕的次数
@State mode: power.DevicePowerMode | string = "" //获取当前设备的电源模式
@State isStandby:boolean = false; //记录当前设备是否进入待机低功耗续航模式,始终为false,暂时不知道怎样去触发(在屏幕休眠与省电模式下判断过)
//监听全屏的触屏事件,当屏幕休眠时并监测到用户双击屏幕时,解除屏幕休眠状态
watchTouch() {
try {
inputMonitor.on('touch', (touchEvent: TouchEvent) => {
console.log("ScreenSleep", "watchTouchFun", `Monitor on success ${JSON.stringify(touchEvent)}`)
try {
console.log("ScreenSleep", "watchTouchFun", "touchCount:", this.isActive)
//可以添加一个延时唤醒,计划当双击屏幕时,接触屏幕休眠状态(注:同时也得防止用户多次敲击屏幕造成一定的程序紊乱)
!this.isActive ? (this.touchCount++ == 2 && this.delayWakeup()) : this.isActive = power.isActive()
console.log("ScreenSleep", "watchTouchFun", "touchCount:", this.touchCount)
} catch (err) {
console.error("ScreenSleep", "watchTouchFun", 'wakeup failed, err: ' + err)
}
return false
});
} catch (error) {
console.log("ScreenSleep", "watchTouchFun", `Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`)
}
}
//取消全屏的触屏事件的监听
unWatchTouch() {
try {
inputMonitor.off('touch');
console.log("ScreenSleep", "unWatchTouchFun", `Monitor off success`)
} catch (error) {
console.log("ScreenSleep", "unWatchTouchFun", `Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`)
}
}
//监测当前设备是否处于活动状态。有屏的设备为亮屏状态,无屏的设备为非休眠状态
watchIsActive() {
//当监测到当前设备为亮屏状态时,重置触屏次数为0
if (this.isActive) {
this.touchCount = 0;
this.unWatchTouch()
}
}
//屏幕休眠
screenSuspend() {
try {
if (this.isActive) {
power.suspend();
this.mode = power.getPowerMode();
this.isStandby = power.isStandby();
console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
console.info("ScreenSleep",'power mode: ' + this.mode);
this.watchTouch()
}
console.log("ScreenSleep", "screenSuspend", "set power suspend success")
} catch (err) {
console.error("ScreenSleep", "screenSuspend", 'suspend failed, err: ' + err)
}
}
//屏幕延时唤醒
delayWakeup(): Promise<boolean> {
return new Promise((resolve, reject) => {
let tid = setTimeout(() => {
//唤醒屏幕休眠状态
power.wakeup('wakeup_touch')
this.isActive = power.isActive()
console.log("ScreenSleep", "delayWakeup", "wakeup success")
if (this.isActive) {
clearTimeout(tid);
}
}, 5000)
})
}
//电源模式
powerMode(){
power.setPowerMode(power.DevicePowerMode.MODE_EXTREME_POWER_SAVE)
.then(() => {
console.info("ScreenSleep", "delayWakeup",'set power mode to MODE_POWER_SAVE 省电模式');
this.isStandby = power.isStandby();
console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
})
.catch((err : Error)=> {
console.error("ScreenSleep", "delayWakeup",'set power mode failed, err: ' + err);
});
}
aboutToAppear(): void {
this.isActive = power.isActive()
console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
}
aboutToDisappear(): void {
}
build() {
Column() {
Button("点击屏幕休眠")
.onClick((event: ClickEvent) => {
this.screenSuspend()
})
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Start)
}
}
复制代码
截图:休眠前
(, 下载次数: 5)
上传
点击文件名下载附件
休眠唤醒后
(, 下载次数: 3)
上传
点击文件名下载附件
state状态变成了background
我尝试过的解决方法和结果
有想过是因为state变为background的原因,但是也没找到具体的方法去实现后台往前台的切换
我想要达到的结果
当唤醒设备后,应用的功能都可以生效
【运行环境】
硬件:
ROM版本:未知
DevEvoStudio版本:4.1
SDK版本:4.1
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5