Android Getlastknownlocation Returns Null

getLastKnownLocation returns null

Use this method to get the last known location:

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();

private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}

getLastKnownLocation null - how to get data?

Thanks everyone for the help!

I was missing uses-features declarations in my manifest - thanks @CommonsWare for the link: https://developer.android.com/guide/topics/location/strategies.html#java

<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />

Also, requesting updates like this is needed as well:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

In my case the GPS provider needs a few minutes to connect and start sending data - the Network provider seems to be faster but does not always work - so I'll use both of them.

Update: a few key points

  1. It's not really intuitive, but in order to get data from getLastKnownLocation some app on the device - be it Google Maps or our own app - needs to call requestLocationUpdates.
  2. Some providers might not be enabled on a particular device - so it's a good idea to use both the GPS_PROVIDER and the NETWORK_PROVIDER - or use all enabled providers: locationManager.getProviders(true)

Here is the code I ended up using:

for ( String provider : locationManager.getProviders(true) ) {
setLocation( locationManager.getLastKnownLocation(provider) );
locationManager.requestLocationUpdates(provider, Tools.MIN_5, 0, locationListener);
}
// call setLocation again in locationListener.onLocationChanged

getLastKnownLocation always returns null in Android 10, after code was working fine in Android 9

Adding the Manifest.permission.ACCESS_BACKGROUND_LOCATIONpermission caused the above code to work again as is.

https://developer.android.com/training/location/permissions#background

getLastKnownLocation returning NULL

you are mixing the new and the old location API's together when you shouldnt.

to get the last known location all your have to do is call

mLocationClient.getLastLocation();

once the location service was connected.

read how to use the new location API

http://developer.android.com/training/location/retrieve-current.html#GetLocation

getLastKnownLocation() returns null

Is there anything I'm missing on how to solve this?

GPS radios are powered down normally, as they are major battery drain. Hence, getLastKnownLocation() can frequently return null or a stale location, because nothing is checking for location fixes. getLastKnownLocation(), therefore, is only useful if you have a casual interest in the location and are happy if there is no location.

If you need to know the location, you will need to use requestLocationUpdates() or similar stuff, to cause Android to power on the GPS radio and actively try to find the location.

getLastKnownLocation first time return null issue

this isn't a bug but simply the Google politics.
The getLastKnowLocation() method return the last location that google service has retrieved.
As Google Documentation says:

The location object may be null in the following situations:

  • Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because

    disabling location also clears the cache.
  • The device never recorded its location, which could be the case of a new device or a device that has been restored to factory
    settings.
  • Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested
    location after the services restarted.

This is why if you switch from Google Maps to your app, you can get last location.
To get actual position without pass from an app to other, you need to implement a new client and request location updates yourself. For more information, see Receiving Location Updates.

I hope I have clarified your every doubt. Otherwise, do not hesitate to write :)

locationManager.getLastKnownLocation() return null

I had this exact same problem. It was because my device was not storing a last known location. I simply went on to Google Maps and pinpointed my location with GPS, then a value was returned for getLastKnownLocation()



Related Topics



Leave a reply



Submit