0

The method which I am using right now is very unreliable. In my Android device(not simulator) most of the times the location returned is null .Though sometimes it does work.

 public Location getLocation(String provider) {
    if (locationManager.isProviderEnabled(provider)) {
        locationManager.requestLocationUpdates(provider,
                MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(provider);
            return location;
        }
    }
    return null;
}

I know that after the introduction of API 23 there are other permissions I need to take care of. Is there a reliable way to get location ?

user3450804
  • 83
  • 1
  • 6
  • http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a – since k saji Apr 09 '16 at 20:29
  • http://stackoverflow.com/questions/15997079/getlastknownlocation-always-return-null-after-i-re-install-the-apk-file-via-ecli – since k saji Apr 09 '16 at 20:35

2 Answers2

0

Believe me, the most reliable way to get location is relying on 'onLocationChanged' method, because calling getLastKnownLocation method does not mean you get a fresh location or not null result. You can read the documentation below.

This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.

If the provider is currently disabled, null is returned.

And last thing that i want to notice you, calling requestLocationUpdates method does not mean it will give you location asap. So this is why i mentioned you 'onLocationChanged' callback method.

blackkara
  • 4,900
  • 4
  • 28
  • 58
0

Checkout the google samples on github related to location:

https://github.com/googlesamples/android-play-location

How to get last known location, location updates and more

Zeyad Gasser
  • 1,516
  • 21
  • 39