Reputation: 549
According to https://github.com/ReactiveX/RxAndroid/issues/257#issuecomment-164263215 . interval is just for active code, and if app is not wake up, it will not work. So how to use interval for background scheduling tasks?
Upvotes: 1
Views: 1952
Reputation: 13321
Please DO NOT use this solution:
To use interval
from RxJava
you'll have to make sure your app's process stays alive. One way to do it is to put use the Observable
in a foreground service. This is a bad idea because the service is NOT actively delivering value to the user. Waiting for time to pass is not delivering value for the user. Again please DO NOT use this.
AlarmManager and JobScheduler (or it's backport GcmNetworkManager) are far better choices for repeating background activities. If you use AlarmManager.setInexactRepeating()
the system can batch jobs from multiple apps together to save battery. Using JobScheduler
enables you to execute your background jobs in specific conditions, eg. when the device is connected to the internet or when battery is more than 20%. (Internet is required to check the weather).
interval
from RxJava
does have it's usage on Android. It's an excellent replacement for Runnable.postDelayed
for relatively short durations. It makes the code shorter and more readable.
Upvotes: 3
Reputation: 4447
If you need to schedule a task that should be run even if app is not active anymore then use AlarmManager
.
If you need to schedule a task that should be run only when app is active then you can use Observable.interval()
and react on emission to execute some code and please don't forget to unsubscribe from the Observable
when appropriate (when Activity
is paused, etc) so app won't burn the battery!
Upvotes: 0