question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:11

github_iconTop GitHub Comments

3reactions
mfleungaccommented, Aug 4, 2019

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.

1reaction
juanamdcommented, May 27, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Android] Stop working when phone is locked #15 - GitHub
But when app has been installed by .apk file then timer is stopped after 30-40 seconds (when phone is locked).
Read more >
React-native-background timer stop working after 15 minutes ...
Android automatically kills your app after a certain time of inactivity so you cannot run a endless process in background.
Read more >
Tracks stop playing after my Android device goes to sleep mode.
Launch Android settings; Select Wireless and Networks; Select Data Usage; Tap the 3DOTS on the top right; Tap 'Restrict Background Data' and disable...
Read more >
Interval timer stops working when screen locks. - WEBcazine
Im using an interval timer when working out. Basically i need a beep every 60 seconds. When lock screen comes on, it stops...
Read more >
Is it possible the timer works when phone locked? - MSDN
Timers.Timer in core layer, cuz I'm going to make an android ... to keep it working when device locked or application in background??...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found