I'm using fused location provider in my app which is set to PRIORITY_HIGH_ACCURACY
at an interval of 60 seconds. This usually works but sometimes the location doesn't update for 5 to 10 minutes by itself, and I when I open Google Maps, it seems to trigger a location update.
Anyone else here who might have run into this issue or knows of the possible causes behind this behavior? Is there any way to fix this?
My findings from the following link the Fused Location Provider will only maintain background location if at least one client is connected to it. Once the first client connects, it will immediately try to get a location. If your activity is the first client to connect and you call getLastLocation()
right away in onConnected()
, that might not be enough time for the first location to come in. This will result in the location being null
.
To solve this issue, you have to wait (indeterminately) till the provider gets the location and then call getLastLocation()
, which is impossible to know.
Another (better) option is to implement the com.google.android.gms.location.LocationListener
interface to receive periodic location updates (and switch it off once you get the first update).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With