Keep app working even when phone is locked/sleep - Ionic4

旧巷老猫 提交于 2020-03-04 05:06:12

问题


App is an ionic4 capacitor app which uses few cordova plugins as well. I have a timer feature associated with a bluetooth device that has to keep running even when the phone goes to sleep or in locked mode. Using the cordova-background-mode plugin the app works in the background, but as soon as phone is locked or in sleep the timer stops working. When the screen resumes, the timer continues from where it stopped.

I cannot use the platform pause and resume events in my case as just taking the difference and adding up with the time will provide wrong values with my use case.

This app is used with the height adjusting table, where a user can use the app to adjust the height and also track the time when he is in standing position. (The timer starts running when the table reaches a certain height). The height adjusting table also has button to control its height.

Use case (Why I cannot use the platform resume and pause events along with timestamps) :

  1. Desk is in standing position. Timer starts running.
  2. I lock my phone (timestamp created)
  3. I go in sitting position for 1h (The table also has buttons to control the height, so this is referred to when the user uses that to reduce the height and is in sitting position, but the phone is still locked/sleep)
  4. I go back into standing position for 20min
  5. I unlock my phone. (Second time stamp created)
  6. Now it will show 1:20min standing time even though I stood only 20min

How else can I make my app timer running even if the phone is locked? Or should I use the insomnia cordova plugin to keep it awake when the timer is running? What happens when a user manually locks the phone even with the insomnia plugin? Will it keep awake?


回答1:


I got it working using the wakeup method from the cordova background mode plugin inside the ble's startNotification method. @Paulw11's suggestion helped!



来源:https://stackoverflow.com/questions/60056998/keep-app-working-even-when-phone-is-locked-sleep-ionic4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!