Can I keep a timer running while app is in background

  • 1
  • Question
  • Updated 2 years ago
I want my app to timeout if there is no activity for 5-10 minutes. I have implemented this logic, where the timer starts running as soon as my user has logged and it works as expected in the foreground so my logout web service is called when the timer reaches its limit.

My problem is that the timer stops as soon a the app is put in the background. Is it possible to implement this function while the app is in the background?

Also another background related issue I have, I use geolocation in my app and when the app has network connection I use watchPosition which keeps going in the background, but I also have some logic when there is no network.

I have limited functionality so I use getCurrentPosition and save to my local database to sync later. I also have a timer running here, so I have the same issues that it stops while in the background.

Should I change to watchPosition so my app will keep the timer running in the background?
Photo of Philip Kelly

Philip Kelly

  • 8 Posts
  • 0 Reply Likes
  • unsure

Posted 5 years ago

  • 1
Photo of Marco Milone

Marco Milone

  • 49 Posts
  • 1 Reply Like
put

< preference name="KeepRunning" value="true" / >
< preference name="exit-on-suspend" value="false" / >
(without spaces after "" )

in config.xml

Android will not stop javascript background execution.
Then, Android can close your app while in background for any reason.
Photo of Sarvesh Admuthe

Sarvesh Admuthe

  • 1 Post
  • 0 Reply Likes
Thanks Man ! It helped me ! Cheers !
Photo of Philip Kelly

Philip Kelly

  • 8 Posts
  • 0 Reply Likes
Thank you for the reply, I am sorry I should have been more specific. I am currently working on an iOS build of the app so I am more interested to know how I can get that to work in the background.

I will keep your answer in mind when I am working on the Android build.

Thank you
Photo of Yi Ming Kuan

Yi Ming Kuan

  • 1840 Posts
  • 78 Reply Likes
You can't run Javascript while the app is in the background, for iOS. You'll have to use a plugin for that instead.

-yiming
Photo of Philip Kelly

Philip Kelly

  • 8 Posts
  • 0 Reply Likes
Thank you for your reply,

Yes I know, I have been looking at plugins to perform this but can not get it working.

I have tried this plugin https://github.com/katzer/cordova-plu...

Which also installs the plugin for device info, there seems to be an issue with this plugin.

Do you where I could find a working example of a background plugin?

Phil
Photo of Martin

Martin

  • 60 Posts
  • 2 Reply Likes
Hi Phil, did you find a solution for that?