【问题标题】:Android GPS Status driving me nutsAndroid GPS 状态让我抓狂
【发布时间】:2011-05-09 20:29:58
【问题描述】:

我正在制作一个使用 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();
    }
}

【问题讨论】:

    标签: android gps


    【解决方案1】:

    改变这个:

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

    您正在设置一个具有非布尔值的布尔值。因此在 gps 修复过程中出现奇怪的图标行为,您需要在某处设置 isGPSfix 布尔值。对于 hasGPSfixdoesnothaveGPSfix..

    你的意思可能是:

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

    【讨论】:

    • 像梦一样工作。有趣的是,我不会想到这一点。感谢您的帮助。
    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2011-05-20
    • 2010-12-29
    • 1970-01-01
    • 2014-05-19
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多