Android GPS status drives me nuts

I am making an application that uses a GPS receiver. The application will work on all versions starting from version 1.6. I have a satellite icon in which I tell users the current status:

  • if the icon is red - GPS is disabled
  • If the icon is orange - gps is on and trying to fix on satellites
  • If the icon is green - gps is fixed and works fine.

After reading here, I found that some events for onLocationChanged launch version 1.6, but not later, therefore, taking advice, I implemented a GPS receiver. I have a very strange behavior, as the status of the icon is confused. For example, I turn on the GPS and turn orange ... after the correction turns green .. after a few seconds, read after the second orange and so on ...

Here is the code I'm using. Please suggest what change

public class TrackExecutionActivity extends Activity{ protected static final long GPS_UPDATE_TIME_INTERVAL=3000; //millis protected static final float GPS_UPDATE_DISTANCE_INTERVAL=0; //meters private LocationManager mlocManager; private MyGPSListener mGpsListener; private LocationListener mlocListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.trackexecution); imgGpsState = (ImageView)findViewById(R.id.imgGpsState); mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); mlocListener = new MyLocationListener(); mGpsListener = new MyGPSListener(); } private class MyGPSListener implements GpsStatus.Listener { public void onGpsStatusChanged(int event) { boolean isGPSFix = false; switch (event) { case GpsStatus.GPS_EVENT_SATELLITE_STATUS: if (mLastLocation != null) isGPSFix = (SystemClock.elapsedRealtime() - mLastLocationMillis) < GPS_UPDATE_TIME_INTERVAL * 2; if (isGPSFix) { // A fix has been acquired. imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_green)); } else { // The fix has been lost. imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_orange)); } break; case GpsStatus.GPS_EVENT_FIRST_FIX: imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_green)); break; case GpsStatus.GPS_EVENT_STARTED: imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_orange)); break; case GpsStatus.GPS_EVENT_STOPPED: imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_red)); break; } } } public class MyLocationListener implements LocationListener { public void onLocationChanged(Location location) { if (location != null) { mLastLocationMillis = SystemClock.elapsedRealtime(); // do some things here mLastLocation = location; } public void onProviderDisabled(String provider) { imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_red)); } public void onProviderEnabled(String provider) { imgGpsState.setImageDrawable(ctx.getResources().getDrawable(R.drawable.gps_on_orange)); } //this doesn't trigger on Android 2.x users say public void onStatusChanged(String provider, int status, Bundle extras) { } } } @Override protected void onResume() { if(mlocManager != null) { if (mGpsListener == null) { mGpsListener = new MyGPSListener(); } if (mlocListener == null) { mlocListener = new MyLocationListener(); } mlocManager.addGpsStatusListener(mGpsListener); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, GPS_UPDATE_TIME_INTERVAL, GPS_UPDATE_DISTANCE_INTERVAL, mlocListener); } super.onResume(); } } 
+4
source share
1 answer

change this:

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

you set a boolean with non-zero values. Therefore, the strange behavior of the icon during gps fixes you need to install isGPSfix somewhere boolean .. for the case hasGPSfix or doesnothaveGPSfix ..

Did you mean:

 if ((SystemClock.elapsedRealtime() - mLastLocationMillis) < GPS_UPDATE_TIME_INTERVAL * 2) { isGPSFIX = true; } 
+7
source

All Articles