ThemuRR
ThemuRR

Reputation: 61

Android Location Manager returning NULL

I have a simple location manager that normally works, however when the Android device has been turned off and then turned back on, the android location manager returns Null even when I have it requesting updates. I am aware that getLastKnownLocation can return null, however I believe I am handling that in my code. All suggestions Appreciated.

Apparently lon = location.getLongitude(); is crashing it.

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
        {
            // request location update!!
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lon = location.getLongitude();
            lat = location.getLatitude();
        }


        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //Get last known location
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //Update if not null
        if (location != null)
        {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        //Request update as location manager can return null otherwise
        else
        {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
    }

Upvotes: 0

Views: 2017

Answers (3)

Ohad Zadok
Ohad Zadok

Reputation: 3620

I saw your reply on the airplane mode but you do however have bad practices in your code.

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
        {
            // request location update!!
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lon = location.getLongitude();
            lat = location.getLatitude();
        }

you already saw here that location in null, that means that you can't access the inner properties such as latitude and longitude. In any case, location requests are asynchronous and it takes a short amount of time for them to start.

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //Get last known location
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //Update if not null
        if (location != null)
        {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        //Request update as location manager can return null otherwise
        else
        {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
    }

Even here, in the "else" clouse, you can't access the location right away. that means that you can't immediately get the location (it will just hold the last value).
What you should do is implement the onLocationChanges method and hadle the location there in asynchronous way. like this:

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null)
    {
        lat = location.getLatitude();
        lon = location.getLongitude();
    }
   else
    {
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

}

@Override
public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lon = location.getLongitude();
}

Upvotes: 2

ThemuRR
ThemuRR

Reputation: 61

The only problem was that I had airplane mode enabled on the Android Phone. I'm a bit embarrassed now!

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007554

the android location manager returns Null even when I have it requesting updates

Correct. Requesting updates requests that Android start trying to find out where the device is. That will take a while. In the meantime, getLastKnownLocation() can very well return null.

getLastKnownLocation() is generally only useful if you would like to know the location, but if that data is not ready, you can move on without it. If you need the location, and getLastKnownLocation() returns null, you will to wait to use the Location until onLocationChanged() is called. Note that onLocationChanged() may never be called, as there is no requirement that the user have any location providers enabled, and there is no requirement that the device be in a position to actually get location fixes.

Upvotes: 2

Related Questions