Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gps

How can I check the current status of the GPS receiver? I already checked the LocationListener onStatusChanged method but somehow it seems that is not working, or just the wrong possibility.

Basically I just need to know if the GPS icon at the top of the screen is blinking (no actual fix) or solid (fix is available).

like image 789
nr1 Avatar asked Jan 07 '10 15:01

nr1


People also ask

How can I check my GPS status?

To begin, open your app drawer and type “Compass” in the search bar (unless you're using a Samsung, then pull from the side of the screen to access your edge panel). If your phone comes equipped with a native compass, it will show up here.


1 Answers

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:

@Override public 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.

like image 95
soundmaven Avatar answered Oct 27 '22 03:10

soundmaven