Per the getLastKnownLocation documentation:
If the provider is currently disabled, null is returned.
as you are using getBestProvider(criteria, false)
you are saying you allow providers that are not enabled (that's what false
means) - switch it to true
if you only want to look at enabled providers (which will assure that getLastKnownLocation does not return null).
Note that the getLastKnownLocation
could be very out of date and you may still want to look for location updates if you need to get a recent location.