应用调用接口后,HIlog显示有报错 

01-17 10:47:06.403 3602-4003/? I C02907/PowerSuspend: [power_mgr_service.cpp:461] SuspendDevice# Try to suspend device, pid: 4527, uid: 20010048
01-17 10:47:06.403 3602-4003/? I C0290a/PowerState: [power_state_machine.cpp:1032] TransitTo# Transit from AWAKE to INACTIVE for APPLICATION ignoreLock=1
01-17 10:47:06.403 3602-4003/? I C0290a/PowerState: [power_state_machine.cpp:126] operator()# StateController_INACTIVE lambda start
01-17 10:47:06.403 3602-4003/? I C02982/DisplayPowerSvc: [display_power_mgr_service.cpp:417] RegisterCallback# RegisterCallback
01-17 10:47:06.404 3602-4003/? E C02981/DisplayPowerFwk: [display_power_mgr_service.cpp:425] RegisterCallback# Callback is not proxy
01-17 10:47:06.404 3602-4003/? I C0290a/PowerState: [device_state_action.cpp:99] SetDisplayState# Register Callback is 0

应用调用接口后,系统锁屏。手动解锁后应用再次调用suspend时,系统不会锁屏,log显示

01-17 10:49:13.543 3602-3642/? I C0290a/PowerState: [power_state_machine.cpp:1032] TransitTo# Transit from INACTIVE to INACTIVE for APPLICATION ignoreLock=1

此时,应用调用 power.wakeup 后 再次调用 suspend,系统再次进入锁屏界面。

 

附页面ets全部代码

import power from '@ohos.power'
import systemTimer from '@ohos.systemTimer';
import window from '@ohos.window';
import common from '@ohos.app.ability.common';

function   delay(ms:number){
  return new Promise(res=>setTimeout(res, ms))
}

@Entry
@Component
struct Index {

  context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
  async setSystemBar() {
    let windowClass = await window.getLastWindow(this.context)
    //设置导航栏,状态栏不可见
    await windowClass.setWindowSystemBarEnable([])
  }

  aboutToAppear() {
    this.setSystemBar()
  }

  @State name:string = "关机与待机测试应用 v0.0.1"
  @State isWorking:boolean = false
  @State result:string = ""

  act(action:Function) {
    return async ()=>{
      if(this.isWorking) {
        return;
      }
      this.isWorking = true
      await delay(100)
      try{
        await action()
        this.result = "工作正常"
      } catch(err) {
        this.result = err.toString()
      }
      this.isWorking = false
    }
  }

  @State actions:{name:string, action:()=>PromiseLike<void>}[] = [
    {name: '息屏待机', action:this.act(async()=>{
      let oldMode = power.getPowerMode()
      await power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE);
      power.suspend()
      await delay(100)
      power.wakeup('prevent suspend loop by app')
      await power.setPowerMode(oldMode)
    })},
    {name: 'STR待机', action:this.act(async()=>{
      let oldMode = power.getPowerMode()
      await power.setPowerMode(power.DevicePowerMode.MODE_NORMAL);
      power.suspend()
      await delay(100)
      power.wakeup('prevent suspend loop by app')
      await power.setPowerMode(oldMode)
    })},
    {name: '关机', action:this.act(async()=>power.shutdown('user command via app'))},
    {name: '重启', action:this.act(async()=>power.reboot('user command via app'))},
  ]

  @State show_hidden:boolean = false
  changeShowHidden(){
    this.show_hidden = !this.show_hidden;
  }
  @State hidden_actions:{name:string, action:()=>PromiseLike<void>}[] = [
    {name: '待机', action:this.act(async()=>power.suspend())},
    {name: '唤醒', action:this.act(async()=>power.wakeup('user command via app'))},
    {name: '标准模式', action:this.act(async()=>power.setPowerMode(power.DevicePowerMode.MODE_NORMAL))},
    {name: '省电模式', action:this.act(async()=>power.setPowerMode(power.DevicePowerMode.MODE_POWER_SAVE))},
    {name: '性能模式', action:this.act(async()=>power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE))},
    {name: '极致省电', action:this.act(async()=>power.setPowerMode(power.DevicePowerMode.MODE_EXTREME_POWER_SAVE))},
    {name: '待机1m唤醒', action:this.act(this.suspend_1m_then_awake)},
    {name: '关机1m开机', action:this.act(this.shutdown_1m_then_power_up)},
    {name: '工作10秒', action:this.act(this.delay_10s_with_system_timer)},
    {name: 'test', action:this.act(this.log_and_after_10s_log)},
  ]

  async suspend_1m_then_awake(){
    // 3.2 不支持直接进入待机,只能先熄屏
    // 进入极限省电模式,以在进入熄屏后1秒立刻待机
    // 保存当前电源模式,供唤醒后恢复
    const oldMode = power.getPowerMode()
    await power.setPowerMode(power.DevicePowerMode.MODE_EXTREME_POWER_SAVE)
    const timer = await systemTimer.createTimer({
      type: systemTimer.TIMER_TYPE_WAKEUP,
      repeat: false,
      callback: ()=>void 0,
    })
    await systemTimer.startTimer(timer, 10*60*1000)
    power.suspend()                             // 这里待机
    await systemTimer.destroyTimer(timer)       // ?这行代码是否有必要
    // 函数执行完毕10分钟后,系统会自动唤醒吗?
    await power.setPowerMode(oldMode)           // 还原电源模式
  }

  async shutdown_1m_then_power_up(){
    const timer = await systemTimer.createTimer({
      type: systemTimer.TIMER_TYPE_WAKEUP,
      repeat: false,
      callback: ()=>void 0,
    })
    await systemTimer.startTimer(timer, 1*60*1000)
    power.shutdown('user command via app')                            // 这里关机
    await systemTimer.destroyTimer(timer)       // ?这行代码是否有必要
  }

  async delay_10s_with_system_timer(){
    return await new Promise(async res=>{
      const timer = await systemTimer.createTimer({
        type: systemTimer.TIMER_TYPE_REALTIME,
        repeat: false,
        callback: ()=>res(void 0),
      })
      const target = new Date().getTime()+10*1000
      await systemTimer.startTimer(timer, target)
    })

    // return await new Promise(async res=>{
    //   setTimeout(()=>res(void 0), 10*1000)
    // })
  }

  async log_and_after_10s_log(){
    let options: systemTimer.TimerOptions = {
      type: systemTimer.TIMER_TYPE_REALTIME,
      repeat: false,
      callback: () => {
        console.info('test1111', '定时器触发成功')
      }
    }

    systemTimer.createTimer(options)
      .catch((err)=>{
        console.error('test11111', 'error: ' , JSON.stringify(err))
        console.error('test11111', 'errorproto: ' , JSON.stringify(err.__proto__))
        console.error('test11111', 'errorcode: ' , JSON.stringify(err.code))
        console.error('test11111', 'errormessage: ' , JSON.stringify(err.message))
        throw err
      })
      .then(timerId => {
        console.info('test1111', '定时器创建成功 ')
        return systemTimer.startTimer(timerId, new Date().getTime() + 10000)
      })
      .then(() => {
        console.info('test1111', '定时器开启成功')
      })
      .catch((err) => {
        console.error('test11112', 'error: ' , JSON.stringify(err))
        console.error('test11112', 'errorproto: ' , JSON.stringify(err.__proto__))
        console.error('test11112', 'errorcode: ' , JSON.stringify(err.code))
        console.error('test11112', 'errormessage: ' , JSON.stringify(err.message))
      })
  }

  all_lists(){
    return [
      ...this.actions,
      ...(this.show_hidden?this.hidden_actions:[]),
      ]
  }

  build() {
    Flex({wrap: FlexWrap.Wrap, alignContent: FlexAlign.SpaceBetween, direction: FlexDirection.Row, alignItems:ItemAlign.Stretch}) {
      Divider()
      Flex({justifyContent: FlexAlign.Center}){
        Text(this.name)
          .fontSize(10)
          .fontWeight(FontWeight.Bold)
      } .width('100%')
      Flex({wrap: FlexWrap.Wrap, justifyContent: FlexAlign.Center}){
        if(!this.isWorking) {
          ForEach(this.all_lists(), (x:{name:string, action:()=>PromiseLike<void>})=>{
            Button(x.name)
              .margin('10vp')
              .onClick(()=>x.action())
              .fontSize('24vp')
          }, (x:{name:string, action:()=>PromiseLike<void>})=>x.name)
        } else {
          Text('正在处理中,请稍后')
            .fontSize(80)
            .fontWeight(FontWeight.Bold)
            .width('100%')
            .textAlign(TextAlign.Center)
        }
      } .width('100%')
      Text(this.result)
        .fontSize(25)
        .height('30vp')
        .backgroundColor('#777777')
        .width('100%')
        .textAlign(TextAlign.Center)
        .onClick(()=>this.changeShowHidden())
    }
    .width('100%')
    .height('100%')
  }
}

 

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐