Basically I am stuck because the "improvements" in battery added in Android 6.0+. My app is supporting API from 11+
Scenario
Here's my scenario: my app fetches data from a web service of a 3rd party server. Since it is not my server I do not have control over when data is added or removed. The availability of the data varies. I want to be able to fire an IntentService to fetch the data, for instance every 30/60 minutes for the next 6 hours. In this way I can have the data in my app covering the case when the data gets unavailable from the server. Of course this needs to go even if my app is closed.
You may say that this is not friendly with the battery but it is an option for power users to have at hand.
What I did so far
I have struggled a lot with setting Alarms
. While setting a repeating alarm seemed the easiest and most convenient way of having it work, it proved to work reliably only until android 5.0.
Starting with the introduction of Doze
, according to docs, the alarms are postponed to maintenance window
and since these windows come at 1-2-4 and so on hours, it is not suitable for my needs.
I gave up on using set repeating
and tried to use a one time Alarm
which gets rescheduled within the IntentService
execution. From testing setExact
didn't seem to run well but then tried setExactAndAllowWhileIdle
which does indeed fire at the right moment BUT if in Doze
mode, it has no network connectivity access, making it useless.
Even so, I am not sure what happens when an alarm with setRepeating
or setExact
is delayed but the device is awaken before next maintenance window...
Basically now I am stuck, with a mechanism that does not work and I don't know what else to choose given the android versions supported by the app and the requirements.
LE: actually I am not aware of a nice way of setting background jobs on android, even excluding Doze. I mean, we do have the JobScheduler
which seems like a nice thing to do... but hey, it's available only on Android 21 and not backward supported... I mean really, having a simple thing to do and I am already waiting a few days so far.