Magnus
Magnus

Reputation: 1442

LocationManager's location in Android returns null

I'm testing my Android app on a device, and it always fails 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!

Upvotes: 2

Views: 5548

Answers (2)

Farhana Haque
Farhana Haque

Reputation: 1381

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);
}

Upvotes: 3

Zds
Zds

Reputation: 4359

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.

Upvotes: 1

Related Questions