5

What is the best way in Android to get a location provider's status? When registering a LocationListener for location updates, the onStatusChanged(String provider, int status, Bundle extras) callback will be called whenever the status is changed. However, I didn't find a way for getting the current status (AVAILABLE, OUT_OF_SERVICE, TEMPORARILY_UNAVAILABLE) without registering to location updates. The isProviderEnabled method just indicates if the provider is generally turned on or not (in the device's settings), so might return true although the provider is out of service... Is there another way for doing this?

Another question: is it possible to listen for status updates without registering for location updates? I want to listen on a specific provider for locations, but at the same time check if others get available. My current workaround is to set the minimum time and distance to a very high value, but I wonder if there's a better solution...

Thank you for any hints!

Btw: if possible, the solutions should work with Android 1.1

king_nak
  • 11,313
  • 33
  • 58
  • I don't know how to do what you want. But you could get the last known location (`LocationManager.getLastKnownLocation`) and see if it is recent. This would give you a estimate if the provider is available or not. – Jason Hanley Mar 10 '11 at 15:52

1 Answers1

2

Why do you want the GPS status, if you are not interested in the location?

However, you can get at least some aspects of the GPS status like this:

final GpsStatus gs = this.locationManager.getGpsStatus(null);
int i = 0;
final Iterator<GpsSatellite> it = gs.getSatellites().iterator();
while( it.hasNext() ) {
    it.next();
    i += 1;
}
this.gpsSatsAvailable = i;

See http://developer.android.com/reference/android/location/GpsStatus.html You will need at least 4 satellites for an accurate 2D fix, and 5 for a 3D fix (incl. altitude). According to the documentation you should access the status from onStatusChanged() to get consistent information. It works also from outside this method. The linked document explains what problems may occur. Of course, you still need to listen for location updates, because the GPS status is not updated otherwise.

Stefan
  • 4,645
  • 1
  • 19
  • 35
  • Why? In my case because I'm developing a technical app that displays a status screen of various onboard and bluetooth-connected sensors, so having an initial value to display only makes sense. More philosophically, if "status" is important enough to merit its own change-handler, surely it's important enough to be able to query its current value? – GISmatters Dec 01 '15 at 23:16