I need to do some background updates (about every 1 min). I have to use JobService
to do it periodically. But I don't need to run service when app is closed - only when app is running (on foreground).
Is there is a way to do that?
I need to do some background updates (about every 1 min). I have to use JobService
to do it periodically. But I don't need to run service when app is closed - only when app is running (on foreground).
Is there is a way to do that?
You don't need a JobService for that. Simply use normal Service. The OS allows apps in foreground to freely create the service. You can easily start a Service and perform your task. You can use Handlerthread
for background processing or use Handler
for foreground processing.
Note for Android O: If your app goes to background then your Service will be stopped as if you have called stopSelf()
. Remember to clean up the necessary object's state.