Hello,
I’m trying to implement a Foreground Service that has to make a periodic API request and based on the API’s response show a notification. The periodicity might vary from 1 minute to 10 minutes based on the user selection. I am aware of the effect this service might have on the battery but it’s something that the app assumes in order to achieve this functionality.
I had a look over an example on your github @ cw-android-q, ForegroundService.kt
but this one extends LifecycleService
and uses LiveData
. At the moment my implementation is a little bit different. I’m using RxJava
and Retrofit
for my API requests and I’m not using LiveData
.
Let’s say my service is AppService
and it extends DaggerService
. I’ve followed this tutorial for the implementation of my service.
I was wondering if an Observable.interval(X, TimeUnit.MINUTES)
would be a good approach for this. So, AppService
should start and the Observable
should periodically make that API request, when the API responses I will call a function that will display a notification. As per the tutorial, he is launching a coroutine (see line 89).
We’ve also talked during office hours, you might or might not remember and recommended me ScheduledExecutorService
.
Ok, so my question is if I’m on the right path or not and if you have any recommendations regarding this:
- would it be ok to use that
Observable
? - would the foreground service affect the
Observable
? - I assume I have to dispose of that
Observable
inAppService
'sonDestroy
method, right?
Let me know if you have any questions or if I can clarify things for you. And sorry in advance if this is not well redacted.