我正在制作一个使用GPS接收器的应用程序。应用程序将在1.6版开始的所有版本上运行。我有一个卫星图标,可以告诉用户当前状态:
  
  
   
    - 
     如果图标为红色-gps已禁用
    
- 
     如果图标为橙色-gps已启用并尝试修复卫星
    
- 
     如果图标为绿色-gps已修复并且运行正常。
    
  
   在阅读了这里之后,我发现onLocationChanged触发器的一些事件在1.6版本上发生了变化,但不是以后的版本,所以按照建议我实现了一个GPS监听器。当图标的状态变得混乱时,我有一些非常奇怪的行为。例如,我启用GPS,得到橙色。。。修好后,变绿。。几秒钟后,在第二个橘子等之后被读取。。。
  
  
   这是我使用的代码。请提出改变的建议
  
  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();
    }
}