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

Background timer stops after a while when the phone is locked [Android]

Open tws9797 opened this issue 5 years ago • 11 comments

This is the example of the code

var time = 0; 
BackgroundTimer.setInterval(() => {
 time ++;
console.log(time)
}, 1000)

Let say in my log it shows

...
331
332
333
...

Then the timer stop here When I unlocked my phone again and open the app again

...
331
332
333
334
335
336
337
...

The variable of time suddenly increase many times. It seems like the adding process stack and run at once. This happened when I test the app on my device without USB connection. However with USB connection, the problem resolved (most likely is because it is charged when connected with USB ) The phone model is Google Pixel 1 and Android version is 9. The battery optimization is disabled already. Does anybody have this issue as well?

tws9797 avatar Jul 30 '19 14:07 tws9797

Same here. Android 9, samsung a20. Timer stop after 15min the app is in background. It resumes after unlocking the phone just like you said.

mfleungac avatar Aug 04 '19 04:08 mfleungac

The app now is able to run at background when locked, but only with the condition that the phone is charging. If not charged, the timer will stop frequently.

tws9797 avatar Aug 12 '19 21:08 tws9797

Has anybody found a resolution on this?

jacobbeasley avatar Aug 14 '19 21:08 jacobbeasley

same here

sunshowerstack avatar Oct 08 '19 03:10 sunshowerstack

I find a solution in internet but it's not really great...

const monitorProgress = async (dispatch, id) => {
    if (pos >= duration * AUDIO_DONE_AT) {
      BackgroundTimer.stopBackgroundTimer();
      dispatch(addAudioDone(id));
    }
};

const startMonitoring = (dispatch, id) => {
  BackgroundTimer.runBackgroundTimer(() => {
    monitorProgress(dispatch, id);
  }, MONITOR_INTERVAL);
};

reload... the timer

yelkamel avatar Oct 08 '19 17:10 yelkamel

any update on this issue?

HugoLiconV avatar Jan 09 '20 18:01 HugoLiconV

In case anyone runs into this issue, Android seems to shut these kinds of services down if they run more often than every 15 minutes when the phone is locked. I switched my services from every minute to every 15 minutes and now it's working like a charm.

Mattimus333 avatar Jan 17 '20 23:01 Mattimus333

The phone doesn't work after it's black. Have you solved it

AdelaDella avatar Mar 06 '20 02:03 AdelaDella

Same here. Stops working as soon as the screen is locked.

theagilecoder avatar Jul 27 '20 09:07 theagilecoder

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

I has the same problem. When phone is locked the background timer stop work and only returns when I back to the application.

Someone have any solution?

Navicna avatar Sep 01 '22 19:09 Navicna