0

I'm developing an app that checks the weather based on the current location every 3 hours. I'm getting the location using the FusedLocationProviderApi and using a pendingIntent to a BroadCastReceiver that start up an IntentService.

In the FusedLocationProviderApi you can specify an interval period when you are creating the LocationRequest. So if I specify the interval to be 3 hours and the fastest interval as well to be 3 hours (I don't want to get updates before that), what happens if location is not available when it's time to do a location update?

Will I still get the location update intent at the scheduled time? I would like to use the last known location if the location is not available, but I need to be certain that I am still getting the PendingIntent at the scheduled time.

Or is it better to use an alarm manager to handle the periodic work and request the location update from within the IntentService instead?

Thanks

stefanb
  • 221
  • 2
  • 7

1 Answers1

0

In the FusedLocationProviderApi you can specify an interval period when you are creating the LocationRequest. So if I specify the interval to be 3 hours and the fastest interval as well to be 3 hours (I don't want to get updates before that), what happens if location is not available when it's time to do a location update?

In this scnerio device must be awake to keep alive your location request. So it means you must have a non-stop(theoretically) background service, and partial wake lock as well. They sound not good.

Instead, you could refer AlarmManager approach which is set to wake up at each 3 hours. Then idea works like below

  • Device wakes up
  • Makes location request asap (set interval values to zero)
  • Continues to sleep after receiving location (and also doing your actual work)
blackkara
  • 4,900
  • 4
  • 28
  • 58
  • Thanks. I thought that using a PendingIntent for the location update to a WakefulBroadcastReceiver which starts in IntentService that request the weather for the location, would take care of the case if the device is in sleep mode (without having to keep it awake while waiting). – stefanb Dec 16 '16 at 12:56
  • Do you have any experience on how reliable the periodic reporting from the FusedLocationProviderApi is. Is it as reliable as an AlarmManager? – stefanb Dec 16 '16 at 13:00
  • Please check this library [cwac-locpoll](https://github.com/alexbirkett/cwac-locpoll). This is exactly what you need, but uses LocationManager. You can implement fused location provider instead of old one. – blackkara Dec 16 '16 at 13:11