I am making a application that uses the GPS receiver. The application will work on all versions starting from 1.6. I have a satellite Icon in which I tell the users the current status:
- if icon is red – gps disabled
- if icon is orange – gps is enabled and trying to fix on the satellites
- if icon is green – gps is fixed and running fine.
After reading around here, I’ve found that some events for onLocationChanged trigger on 1.6 version but not later, so taking the advice I implemented a GPS listener. I have some really weird behavior as the status of the icon gets messed out. For instance I enable GPS and gets orange… after a fix get’s green.. after a few seconds gets read after a second orange and so on…
Here is the code I use. Please suggest what to 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();
}
}
change this:
your are setting a boolean with non boolean values.. hence the weird icon behavior during gps fixes you need to set that
isGPSfixboolean somewhere.. for case ofhasGPSfixordoesnothaveGPSfix..You might have meant: