0

My app lets people record routes when driving. To ensure locations are actually recorded, i use a service so that they are recorded even if the activity is killed. To avoid the device being put into Doze, which would mean not getting frequent location updates, i hold a wakelock.

Are these steps sufficient? Or do i need to explicitly disable battery optimization ( https://developer.android.com/training/monitoring-device-state/doze-standby.html ) to ensure i dont lose out on location updates? According to the docs, my app seems like an "acceptable" use-case for that.

user1202032
  • 1,430
  • 3
  • 15
  • 36
  • 1
    If you're a foreground service, doze doesn't apply. – ianhanniballake Mar 23 '18 at 00:41
  • @ianhanniballake Just to make sure i understand you correctly, this means a foreground service can hold a FusedLocationClient and continue to receive location updates? If so, please add your comment as an answer and i'll accept it :) – user1202032 Mar 24 '18 at 12:47

1 Answers1

0

Foreground services are not affected by doze - if you want to constantly be running and getting high accuracy location, you should be a foreground service.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • My foreground service is killed after ~30 minutes. It seems like other people on stackoverflow have similar issues (https://stackoverflow.com/questions/6645193/foreground-service-being-killed-by-android). Any suggestions? – user1202032 Apr 02 '18 at 14:12
  • @user1202032 yeah it is a bit difficult to understand under the hood working but Foreground service will usually survive for longer times because that's why we have Foreground service in the first place. Another key aspect to remember is the difference made by `return Service.START_STICKY` in `onStartCommeand()` function. Documentation tells us that using this will re-create the service if it this service's process was killed. – Wahib Ul Haq May 22 '18 at 12:28
  • 1
    @WahibUlHaq You think so? I think its actually fairly simple to understand. Foreground services are not really supposed to be killed - On e.g. Samsung devices this is true, but for OnePlus its not. Same thing with `START_STICKY` - Its supposed to restart your service when possible, which happens on e.g. Samsung but not on OnePlus devices. The documentation is pretty clear, but its not very useful when some OEM's just decide to do something else. Its honestly a joke that Google lets these devices get through their compliance program – user1202032 May 23 '18 at 13:27
  • It's mentioned here that location won't be delivered in doze mode and this is per design. https://issuetracker.google.com/issues/63937937 – Henry Feb 22 '20 at 19:57
  • 3
    @Henry - in deep doze, the device is, by definition, not moving. Devices do not go into deep doze when someone is driving, as per the original question. – ianhanniballake Feb 22 '20 at 21:46