-1

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.

Alin
  • 14,809
  • 40
  • 129
  • 218

1 Answers1

1

If the device is in idle mode (because of the Doze system), it means that the user is not actively using it, so I don't see any problem about deferring your job (update) in the next maintenance window.

The only way for waking up the phone, granting access to internet is using

setAlarmClock()

If you use setAlarmClock(), then the device will exit the idle mode few minutes before the alarm time: this depends on the configuration of the Doze, that you and actually change for testing purpose, using shell commands. You should use setAlarmClock() in exceptional case like reminding important calendar events.

Also, when you set it, an icon (a bell) should be displayed in the status bar: it is the same mechanism used for the system alarm.

Btw, I would discourage this approach, and wait for the next maintenance window. In fact, you should avoid poll mechanism at all.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • Thank you, `setAlarmClock` seems to be the only real option but indeed it's a but out of scope. – Alin Feb 18 '17 at 12:35