OpenHarmony开发者论坛

标题: 使用power系统级的挂起和唤醒api后,应用的功能失效的原因? [打印本页]

作者: luocl    时间: 2025-2-26 16:22
标题: 使用power系统级的挂起和唤醒api后,应用的功能失效的原因?
【问题描述】
问题现象和发生的背景
想通过power系统级的api去使得设备休眠,但是唤醒之后应用的功能会失效。

相关的代码,截图,日志信息
代码:
  1. import power from '@ohos.power'
  2. import inputMonitor from '@ohos.multimodalInput.inputMonitor'
  3. import { TouchEvent } from '@ohos.multimodalInput.touchEvent'

  4. @Component
  5. export default struct ScreenSleep {
  6.   @State @Watch('watchIsActive') isActive: boolean = true //当前设备活动状态标识
  7.   @State touchCount: number = 0 //记录当屏幕休眠状态下,用户接触屏幕的次数
  8.   @State mode: power.DevicePowerMode | string = "" //获取当前设备的电源模式
  9.   @State isStandby:boolean = false; //记录当前设备是否进入待机低功耗续航模式,始终为false,暂时不知道怎样去触发(在屏幕休眠与省电模式下判断过)
  10.   //监听全屏的触屏事件,当屏幕休眠时并监测到用户双击屏幕时,解除屏幕休眠状态
  11.   watchTouch() {
  12.     try {
  13.       inputMonitor.on('touch', (touchEvent: TouchEvent) => {
  14.         console.log("ScreenSleep", "watchTouchFun", `Monitor on success ${JSON.stringify(touchEvent)}`)
  15.         try {
  16.           console.log("ScreenSleep", "watchTouchFun", "touchCount:", this.isActive)
  17.           //可以添加一个延时唤醒,计划当双击屏幕时,接触屏幕休眠状态(注:同时也得防止用户多次敲击屏幕造成一定的程序紊乱)
  18.           !this.isActive ? (this.touchCount++ == 2 && this.delayWakeup()) : this.isActive = power.isActive()

  19.           console.log("ScreenSleep", "watchTouchFun", "touchCount:", this.touchCount)
  20.         } catch (err) {
  21.           console.error("ScreenSleep", "watchTouchFun", 'wakeup failed, err: ' + err)
  22.         }
  23.         return false
  24.       });
  25.     } catch (error) {
  26.       console.log("ScreenSleep", "watchTouchFun", `Monitor on failed, error: ${JSON.stringify(error, [`code`, `message`])}`)
  27.     }
  28.   }

  29.   //取消全屏的触屏事件的监听
  30.   unWatchTouch() {
  31.     try {
  32.       inputMonitor.off('touch');
  33.       console.log("ScreenSleep", "unWatchTouchFun", `Monitor off success`)
  34.     } catch (error) {
  35.       console.log("ScreenSleep", "unWatchTouchFun", `Monitor execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`)
  36.     }
  37.   }

  38.   //监测当前设备是否处于活动状态。有屏的设备为亮屏状态,无屏的设备为非休眠状态
  39.   watchIsActive() {
  40.     //当监测到当前设备为亮屏状态时,重置触屏次数为0
  41.     if (this.isActive) {
  42.       this.touchCount = 0;
  43.       this.unWatchTouch()
  44.     }
  45.   }

  46.   //屏幕休眠
  47.   screenSuspend() {
  48.     try {
  49.       if (this.isActive) {
  50.         power.suspend();
  51.         this.mode = power.getPowerMode();
  52.         this.isStandby = power.isStandby();
  53.         console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
  54.         console.info("ScreenSleep",'power mode: ' + this.mode);
  55.         this.watchTouch()
  56.       }
  57.       console.log("ScreenSleep", "screenSuspend", "set power suspend success")
  58.     } catch (err) {
  59.       console.error("ScreenSleep", "screenSuspend", 'suspend failed, err: ' + err)
  60.     }
  61.   }

  62.   //屏幕延时唤醒
  63.   delayWakeup(): Promise<boolean> {
  64.     return new Promise((resolve, reject) => {
  65.       let tid = setTimeout(() => {
  66.         //唤醒屏幕休眠状态
  67.         power.wakeup('wakeup_touch')
  68.         this.isActive = power.isActive()
  69.         console.log("ScreenSleep", "delayWakeup", "wakeup success")
  70.         if (this.isActive) {
  71.           clearTimeout(tid);
  72.         }
  73.       }, 5000)
  74.     })
  75.   }

  76.   //电源模式
  77.   powerMode(){
  78.     power.setPowerMode(power.DevicePowerMode.MODE_EXTREME_POWER_SAVE)
  79.       .then(() => {
  80.         console.info("ScreenSleep", "delayWakeup",'set power mode to MODE_POWER_SAVE 省电模式');
  81.         this.isStandby = power.isStandby();
  82.         console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
  83.       })
  84.       .catch((err : Error)=> {
  85.         console.error("ScreenSleep", "delayWakeup",'set power mode failed, err: ' + err);
  86.       });
  87.   }

  88.   aboutToAppear(): void {
  89.     this.isActive = power.isActive()
  90.     console.info("ScreenSleep",'device is in standby: ' + this.isStandby);
  91.   }

  92.   aboutToDisappear(): void {

  93.   }

  94.   build() {
  95.     Column() {
  96.       Button("点击屏幕休眠")
  97.         .onClick((event: ClickEvent) => {
  98.           this.screenSuspend()
  99.         })
  100.     }
  101.     .width('100%')
  102.     .height('100%')
  103.     .alignItems(HorizontalAlign.Center)
  104.     .justifyContent(FlexAlign.Start)
  105.   }
  106. }

复制代码
截图:休眠前
(, 下载次数: 5)
休眠唤醒后
(, 下载次数: 3)
state状态变成了background
我尝试过的解决方法和结果
有想过是因为state变为background的原因,但是也没找到具体的方法去实现后台往前台的切换

我想要达到的结果
当唤醒设备后,应用的功能都可以生效

【运行环境】
硬件:
ROM版本:未知
DevEvoStudio版本:4.1
SDK版本:4.1





欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5