Android - FusedLocationClient, requestLocationUpdates() not returning results and availability weird behavior
Asked Answered
P

2

7

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:

  1. onLocationAvailability() is called twice. First time the response is true but onLocationResult() is not called and then I get an availability of false! I had some occasions that I got it called more than once.

  2. The above behavior is observed in multiple devices using Android 6-8. A Pixel 3 with Android 9 behaves as expected providing a location.

  3. 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!!!

  4. 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.

Peonage answered 30/1, 2019 at 10:2 Comment(0)
M
1

I got the same problem, and I used LocationRequest.Builder to create my locationRequest then, GPS signal became stable:

locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000)
                .setWaitForAccurateLocation(false)
                .setMinUpdateIntervalMillis(500)
                .setMaxUpdateDelayMillis(1000)
                .build();
Michaels answered 6/11, 2022 at 18:19 Comment(2)
I don't think that calling the setters is really required. The default values are ok. I would only change them if I had a good reason for it. locationRequest = new LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 1000).build(); should be enough.Instar
This does not answer the main question. Do you have the same weird behavior with the quick access setting versus the LocationSettingsRequest? I have not worked on this in a long time so it could have been Android being weird back then. Who knows. By the way, the Builder was not a thing back then.Peonage
I
0

If Google Maps isn't working either, there's no chance you can get your app working properly. The device is broken or misconfigured. You could reset it to factory settings and try again.

Maybe you shouldn't use null as third parameter of requestLocationUpdates(). I've used Looper.getMainLooper() like Google says. https://developer.android.com/training/location/request-updates

Instar answered 19/1, 2023 at 7:11 Comment(1)
I have not worked on this in quite some time but just to clarify some things so that no one gets confused. The device was taken into consideration, was not the problem. The version of Android back then could have a different behavior if the problem I describe is gone. As for the null vs Looper. These are different versions of the function depending on how you have setup your threading.Peonage

© 2022 - 2024 — McMap. All rights reserved.