2010-11-14 50 views
4

我正在使用的GPS接收器的应用。该应用程序将适用于从1.6开始的所有版本。我有一个卫星图标中,我告诉用户当前状态:的Android GPS状态我发疯

  • 如果图标为红色 - GPS禁用
  • 如果图标为橙色 - 启用GPS,并试图修复该卫星
  • 如果图标为绿色 - 全球定位系统是固定的,运行良好。

这里读书左右后,我发现了一些事件触发onLocationChanged在1.6版本,但不晚,所以服用我实现了一个GPS听众的意见。我有一些非常奇怪的行为,因为图标的状态被搞砸了。例如启用GPS,并得到橙色...修复GET的绿色。经过几秒钟后第二橙后会被读取等等...

这里是我使用的代码。请建议要改变什么

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(); 
    } 
} 

回答

7

改变这一点:

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

你要设置一个布尔非布尔值..因此,在你需要的地方设置isGPSfix布尔GPS定点怪异图标的行为。对于案件的hasGPSfixdoesnothaveGPSfix ..

您可能意味着:

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

工作就像一个梦。有趣的是,我不会想到这一点。感谢您的帮助。 – Alin 2010-11-17 18:31:51