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
- 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.
- 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_LOCATION
permission 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
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()
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.
Related Topics
How to Execute Something Just Once Per Application Start
How to Get Current Buildtype in Android Gradle Configuration
Executing Multiple Asynctask's Parallely
Map View Draw Directions Using Google Directions API - Decoding Polylines
The Android Emulator Is Out of View, How to Move It
How to Set Minimum Datepicker Date to Current Date
How to Send Notification to Specific Users with Fcm
Android: How to Get a List of All Available Intent Filters
Display Data After Every 10 Seconds in Android
Require a Password to Uninstall/Remove Application
Setting System Time of Rooted Phone
Get Temperature of Battery on Android
Android Device as a Receiver for A2Dp Profile
How to Connect with Remote Debugger
Register to Be Default App for Custom File Type