I'm testing my Android app on a device, and it always f开发者_StackOverflow中文版ails when I use
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
However, if I use NETWORK_PROVIDER instead of GPS_PROVIDER, it works. The currentLocation returns null.
How can I handle this? Is there anything specific I need to put in my locationListener?
Thanks!
GPS_PROVIDER may not work in bounded place, suppose inside a house or something like this. You should program it in such a way that if gps provider gives null location then you use network provider as a alternative although its accuracy is not so high. Here there is another issue. Your code should be as follows:
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
First of all, check that the GPS icon on device status bar has stopped blinking, marking you actually have GPS lock.
The call returns null if the location is not currently known, for example if you lack GPS lock.
精彩评论