After a lot of reading and fighting with it myself I have to ask my better peers.
I have a behavior that I cannot explain with FusedLocationClient
.
I want to get the location (only one instance) for showing some info to the user. For that I call:
mFusedLocationClient.getLastLocation().addOnSuccessListener(...)
If the location of getLastLocation()
is null. I want to "jump start" the client to fetch me one location.
LocationRequest locationRequest = new LocationRequest();
locationRequest.setFastestInterval(5000);
locationRequest.setInterval(10000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Timber.d("OnLocationResult entered");
if (locationResult != null && !locationResult.getLocations().isEmpty())
findCurrentArea();
}
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
Timber.d("OnLocationAvailability entered");
super.onLocationAvailability(locationAvailability);
}
};
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null);
Let me say before hand that I have also used the LocationSettingsRequest
to check the settings and I always get a success.
The weird part:
onLocationAvailability()
is called twice. First time the response is true butonLocationResult()
is not called and then I get an availability of false! I had some occasions that I got it called more than once.The above behavior is observed in multiple devices using Android 6-8. A Pixel 3 with Android 9 behaves as expected providing a location.
If I start Google Maps on the problematic devices with the location on under those circumstances maps cannot show a location either. If I then turn off the location I get the maps location button with a "?" in it. Pressing the button in maps I get a popup, that I should turn on location. OK on that revives the location service in my app too!!!
If I use the failure case of
LocationSettingsRequest
and go for the auto resolve (same popup as Google Maps) then the location triggers properly!!!
So the question is basically... why?
What does the auto resolve popup do differently when turning on the location service from pressing the quick access button. The quick access button also returns a success when checking the settings. Is there another surefire way to request requestLocationUpdates()
?
Thanks in advance.