0

I am currently using FusedLocationProviderClient for requesting location updates in MainActivity in onCreate method. After activity gets created, a BroadcastReceiver registered as location updates listener. But location updates still continues at night since I don't remove location updates from receiver, but I don't need updates at night since they are generally at home. I want to remove updates from receiver at 10PM and restart receiving location updates at 6AM everyday. I thought that AlarmManager would be sufficient but I couldn't find a proper way to handle this problem. I could just ignore updates coming at night in receiver but I don't think that would be a battery efficient solution.

Summary

  1. Request location updates when user opens application first time and handle updates in background with BroadcastReceiver (requestLocationUpdates(...))
  2. Remove location updates from BroadcastReceiver at 10PM everyday since application doesn't need location updates at night (removeLocationUpdates(...))
  3. Request location updates at 6AM everydat since application needs location updates during day at background with BroadcastReceiver
Muhammed Kadir
  • 576
  • 1
  • 5
  • 17

1 Answers1

0
  1. Don't assume that 10PM is when there's least activity. Instead, you could use triggers such as the Activity Recognition API and JobDispatcher to get when there's no movement and when movement starts again. Might be worth looking at.

  2. Take a look at JobDispatcher API. AlarmManager may still work, though JobDispatcher is favoured on Android system over AlarmManager and does have some nice features, such as specify to do actions only when there's an internet connection. If you're targeting Android SDK lower than 5.0 (API 21) (which you probably are) then use Firebase JobDispatcher. This will allow you to get updates at a time, handles international time differences and be able to specify things like wait for when there's internet connection, etc. It's very powerful!

Tim Kist
  • 1,164
  • 1
  • 14
  • 38
  • 1
    Thanks for the answer but since I don't have specific locations, Geofencing wouldn't suit for my project. And documentation doesn't state any depreciation about AlarmManager, that's wrong JobDispatcher is modern alternative only and AlarmManager has fixes for API 19 and above according to the documentation. – Muhammed Kadir Mar 16 '18 at 12:33
  • You're right, Geofencing won't help. I'm not sure how you can check when a device is IDLE. It is one of the constraints in JobDispatcher. Perhaps you could use the Activity recogniser API see https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient &https://developers.google.com/android/reference/com/google/android/gms/location/DetectedActivity – Tim Kist Mar 16 '18 at 12:49