How can I check the current status of the GPS receiver? How can I check the current status of the GPS receiver? android android

How can I check the current status of the GPS receiver?


As a developer of SpeedView: GPS speedometer for Android, I must have tried every possible solution to this problem, all with the same negative result. Let's reiterate what doesn't work:

  1. onStatusChanged() isn't getting called on Eclair and Froyo.
  2. Simply counting all available satellites is, of course, useless.
  3. Checking if any of the satellites return true for usedInFix() isn't very helpful also. The system clearly loses the fix but keeps reporting that there are still several sats that are used in it.

So here's the only working solution I found, and the one that I actually use in my app. Let's say we have this simple class that implements the GpsStatus.Listener:

private class MyGPSListener implements GpsStatus.Listener {    public void onGpsStatusChanged(int event) {        switch (event) {            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:                if (mLastLocation != null)                    isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;                if (isGPSFix) { // A fix has been acquired.                    // Do something.                } else { // The fix has been lost.                    // Do something.                }                break;            case GpsStatus.GPS_EVENT_FIRST_FIX:                // Do something.                isGPSFix = true;                break;        }    }}

OK, now in onLocationChanged() we add the following:

@Overridepublic void onLocationChanged(Location location) {    if (location == null) return;    mLastLocationMillis = SystemClock.elapsedRealtime();    // Do something.    mLastLocation = location;}

And that's it. Basically, this is the line that does it all:

isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

You can tweak the millis value of course, but I'd suggest to set it around 3-5 seconds.

This actually works and although I haven't looked at the source code that draws the native GPS icon, this comes close to replicating its behaviour. Hope this helps someone.


The GPS icon seems to change its state according to received broadcast intents. You can change its state yourself with the following code samples:

Notify that the GPS has been enabled:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("enabled", true);sendBroadcast(intent);

Notify that the GPS is receiving fixes:

Intent intent = new Intent("android.location.GPS_FIX_CHANGE");intent.putExtra("enabled", true);sendBroadcast(intent);

Notify that the GPS is no longer receiving fixes:

Intent intent = new Intent("android.location.GPS_FIX_CHANGE");intent.putExtra("enabled", false);sendBroadcast(intent);

Notify that the GPS has been disabled:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");intent.putExtra("enabled", false);sendBroadcast(intent);

Example code to register receiver to the intents:

// MyReceiver must extend BroadcastReceiverMyReceiver receiver = new MyReceiver();IntentFilter filter = new IntentFilter("android.location.GPS_ENABLED_CHANGE");filter.addAction("android.location.GPS_FIX_CHANGE");registerReceiver(receiver, filter);

By receiving these broadcast intents you can notice the changes in GPS status. However, you will be notified only when the state changes. Thus it is not possible to determine the current state using these intents.


new member so unfortunately im unable to comment or vote up, however Stephen Daye's post above was the perfect solution to the exact same problem that i've been looking for help with.

a small alteration to the following line:

isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < 3000;

to:

isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < (GPS_UPDATE_INTERVAL * 2);

basically as im building a slow paced game and my update interval is already set to 5 seconds, once the gps signal is out for 10+ seconds, thats the right time to trigger off something.

cheers mate, spent about 10 hours trying to solve this solution before i found your post :)