I'm developing an app that requires the user's movements to be tracked continuously - using a foreground service. The app should work even when it's in background and screen is off.
I don't really need to track it in real time, so, to avoid draining battery, my first solution was a scheduled thread that would fire every 1 minute, fire a location listener, get a location and then turn it off.
This works well. But of course I eventually found out that after Doze mode kicks in, it would defer that task to many minutes - sometimes hours - ahead. Which kind of kills the purpose of my app.
After some research and trials I found that I could use the AlarmManager
for that by calling setExactAndAllowWhileIdle
, which does a better job but still suffers from the same issue.
Then I found that setAlarmClock
circumvents Doze mode consistently (at least on my test device: a Pixel 3a). One thing that it does differently, though, is that it adds a "clock" icon to the status bar indicating that there's an alarm set up.
Now, I can see why Doze mode exists (abuse) and why setAlarmClock
works around it (we need alarm apps to be reliable). And for that reason, I'm not sure if I should proceed with using it and if it's even something that could get my app blacklisted or if it's a bad practice overall - using alarms to retrieve location definitely looks wrong, but documentation on the matter is pretty incomplete at the moment.
I can say this method drains VERY little battery - as little as 1% for 24h usage. And it's probably the best solution for my use case.
So, is it ok to keep using this method? Any chances this could backfire somehow?