I have a background service that lasts approximately 5 minutes and executes X methods every X time each, it is necessary that these times be fulfilled and be consecutive.
The problem is that android 7 kills the service on "turn off screen".
Can I do this with a JobScheduler? (the service has timers inside), or should I use a foreground service?
Example of my service:
public class MyServide extends Service {
class MyBinder extends Binder {
MyServide getService() {
return MyServide.this;
}
}
public IBinder onBind(final Intent intent) {
if (intent != null) {
Observable.timer(randomTime), TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(aLong -> {
//random code
});
}
return MyBinder;
}
}
In order for your service to continue running in the background and not important if you close the application, you must override the method
onStartCommand()
using the START_STICKY property :That is to say that if we close the application that started this service, the service continues its operation.
In the case of stopping the service, this property does not affect being able to stop it.
SOURCE : https://developer.android.com/guide/components/services?hl=es