getLastKnownLocation returns null

The question:

I’ve read some questions about this, but I didn’t quite find an answer that I needed.

So, the case is that I have my map set up, and I want to grab my current gps location.
I have checked that my variables is not NULL but yet my result from:

getLastKnownLocation(provider, false);

Gives me null though, so this is where I need help.
I have added permissions for COARSE + FINE location. But I usually have all kinds of network data disabled for my phone, because I’m not very happy about unpredictable dataflow expenses in my phone bill. So I have only WiFi enabled and connected for this test.

Is there anything more I NEED to enable in order for this to be possible? I would think WiFi should be sufficient?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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) {
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
    return bestLocation;

Method 2

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

compile ""

once the location service was connected.

read how to use the new location API

 private void getLastLocationNewMethod(){
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
            .addOnSuccessListener(new OnSuccessListener<Location>() {
                public void onSuccess(Location location) {
                    // GPS location can be null if GPS is switched off
                    if (location != null) {
            .addOnFailureListener(new OnFailureListener() {
                public void onFailure(@NonNull Exception e) {
                    Log.d("MapDemoActivity", "Error trying to get last GPS location");

Method 3

You are trying to get the cached location from the Network Provider. You have to wait for a few minutes till you get a valid fix. Since the Network Provider’s cache is empty, you are obviously getting a null there..

Method 4

Add this code

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

before the function:

locationManagerObject.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new LocationListener())
as well as the function: locationManagerObject.getLastKnownLocation(LocationManager.GPS_PROVIDER)

Method 5

Try this it works good for me :-

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();

        private Location getLastKnownLocation() {
            Location oldLoc;
            while (true){
                oldLoc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (oldLoc == null){
                else {
            return oldLoc;

You can use NETWORK_PROVIDER in place of GPS_PROVIDER for faster results..

Method 6

I had this problem in Xamarin.Android.

Location location = locationManager.GetLastKnownLocation(provider); was returning null value. I checked my code and got to know that I had just requested permission for ACCESS_COARSE_LOCATION. I added code to request permission for ACCESS_FINE_LOCATION and now it was not returning null. Here is my code:

void AskPermissions()
    if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted ||
            CheckSelfPermission(Manifest.Permission.AccessFineLocation) != (int)Permission.Granted)
        RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    if (CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) == (int)Permission.Granted &&
            CheckSelfPermission(Manifest.Permission.AccessFineLocation) == (int)Permission.Granted)
        Log.Info(tag, "Permission not Granted: Please enter desired Location manually.");

void GetLocation()
    locationManager = (LocationManager)GetSystemService(LocationService);
    provider = locationManager.GetBestProvider(new Criteria(), false);
    Location location = locationManager.GetLastKnownLocation(provider);
    if (location != null)
        Log.Info(tag, "Location Lat: " + location.Latitude + " Lon: " + location.Longitude);
        Log.Info(tag, "Location is null");

In case anyone coming from Xamarin.Android (C#) would find it useful. For Java or Android Studio the code would be similar withs some minor Syntax changes like GetLastKnownLocation() will be getLastKnownLocation() as method names in Java start with lowercase letters while in C# method names start with Uppercase letters.

Method 7

I liked this solution to update location:

Update location

In resume, add the condition:

Location myLocation = getLastKnownLocation();
if (myLocation == null) {
      Log.d("GPS", "GPS Enabled");

And maybe is some basic, but check that GPS is ON.

Method 8

private void getLastLocation()
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {


        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}



// This will help you for fetching location after gps enable


Method 9

Replace ur line with

Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment