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 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() return null and App is shutting down
If you want to get the Location in your activity and if is not needed to run it in a background, you could try this.
package com.jmarkstar.activitylocation;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private MyLocationListener locationListener;
private String latitude;
private String longitude;
@Override
protected void onStart() {
super.onStart();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.ACCURACY_FINE);
String bestEnabledProvider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(bestEnabledProvider, 0, 0, locationListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//YOU CODE
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
makeUseOfNewLocation(location);
locationManager.removeUpdates(locationListener);
}
private void makeUseOfNewLocation(final Location location) {
if (location != null) {
String message = "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy();
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
//YOUR CODE
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
}
}
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() does not necessarily give current location
Have you tried requesting permission to the ACCESS_FINE_LOCATION
provider in your manifest and then from the LocationManager, calling getLastKnownLocation(LocationManager.GPS_PROVIDER)?
It should return to you the last known location acquired by the GPS provider, else null if it can't get the location
Related Topics
How to Trigger Broadcast Receiver When Gps Is Turn On/Off
Convert Magnetic Field X, Y, Z Values from Device into Global Reference Frame
Android Broadcast Receiver for Sent Sms Messages
Connecting 2 Emulator Instances in Android
Using Picasso with Custom Disk Cache
Error Inflating Class Android.Support.V7.Widget.Toolbar
How to Get Cell Service Signal Strength in Android
Pass Arraylist from One Activity to Other
Service Not Available - Geocoder Android
Phonegap - Forcing Landscape Orientation
Firebase: Query to Exclude Data Based on a Condition
Android App Crashing (Fragment and Xml Onclick)
Failed Binder Transaction When Putting an Bitmap Dynamically in a Widget
Android - Get Text Out of Webview