react-native-background-timer icon indicating copy to clipboard operation
react-native-background-timer copied to clipboard

setInterval doesn't wake CPU in deep sleep

Open peacechen opened this issue 5 years ago • 1 comments

setInterval() doesn't hold a wakelock and won't wake the phone if the CPU goes into deep sleep.

There may be a better way to accomplish this on Android. https://stackoverflow.com/questions/17324587/will-a-handler-postdelayed-not-being-fired-when-cpu-sleeps

The recommended pattern for Android's equivalent to cron jobs and Windows scheduled tasks is to use AlarmManager. This works well when coupled with an IntentService, as the service will do its work on a background thread and shut down when there is no more work to do. There's one small problem: IntentService does nothing to keep the device awake. If the alarm was a WAKEUP variant, the phone will only stay awake on its own while the BroadcastReceiver handling the alarm is in its onReceive() method. Otherwise, the phone may fall back asleep. WakefulIntentService attempts to combat this by combining the ease of IntentService with a partial WakeLock.

peacechen avatar Jan 12 '20 22:01 peacechen

Hi, I had this same issue and found no easy solution with this library, so I made my own which works even if the device is locked (among other improvements). Only Android is supported, but you can still use this (or another) library for iOS in conjunction.

https://www.npmjs.com/package/react-native-background-timer-android

I've been using it in my production apps for more than a week with no issues.

juanamd avatar May 27 '21 18:05 juanamd