Well there are a number of things. The GPS could be coming from what's known as a "cold start" where it doesn't have any knowledge of where it is. In these cases the last known location is null. You could also be in a location where there is no signal to get a location fix. It could also be a crappy GPS or a crappy GPS driver (cough samsung cough). These things aren't exact.
First I would start with this documentation. Location Acquisition Strategies
Next, let's evaluate the logic here. Yes the GPS is enabled. However in this context, enabled means that it is enabled for use by applications with the fine location permission. Enabled does not mean it is currently active and acquiring locations. But there is good news! You can make a listener or subscribe to location updates.
So as others have mentioned, make sure you have the permissions setup in your manifest. I assume you do, or else your app probably crashed and burned with a permission issue on the getLastKnownLocation()
call.
Then for receiving location updates, take a look at the LocationListener class. By implementing this interface you can use locationManager.requestLocationUpdates()
to register for location updates from the GPS.
Using your code (I assume some things, like it's within an activity and the method being described is invoked on the UI Thread):
public class MyActivity extends Activity implements LocationListener {
// The rest of the interface is not really relevant for the example
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) { }
// When a new location is available from the Location Provider,
// this method will be called.
public void onLocationChanged(Location location) {
// You should do whatever it was that required the location
doStuffWithLocation(location);
// AND for the sake of your users' battery stop listening for updates
(LocationManager) getSystemService(LOCATION_SERVICE).removeUpdates(this);
// and cleanup any UI views that were informing of the delay
dismissLoadingSpinner();
}
// Then in whatever code you have
public void methodA() {
LocationManager locationManager;
GeoPoint p;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (gps_enabled) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
// When it is null, register for update notifications.
// run this on the UI Thread if need be
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
// Since this might take some time you should give the user a sense
// that progress is being made, i.e. an indeterminate ProgressBar
showLoadingSpinner();
} else {
// Otherwise just use the location as you were about to
doStuffWithLocation(location);
}
}
...
This should run your code as you would expect, and then in the case where the GPS does not have a location, you spin it up and wait until you get a location. Once that occurs you shut it off and use it as you were planning to. This is sort of a naive approach, but you should be able to use it as a simple basis. A real solution should take into account the cases where there is no chance of getting a location, handling the cases when the location provider is not available or becomes unavailable while you are waiting for a location, and validate the sanity of the location.