安卓GPS状态让我抓狂

4
我正在制作一个使用GPS接收器的应用程序,该应用程序将在所有从1.6版本开始的Android系统上运行。我有一个卫星图标,以告知用户当前状态:
  • 如果图标是红色的- 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();
    }
}
1个回答

7

将此更改为:

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

您正在使用非布尔值来设置布尔值。因此,在GPS定位时出现奇怪的图标行为,您需要在某个地方设置isGPSfix布尔值,以进行hasGPSfixdoesnothaveGPSfix的情况。
您可能是想表达:
if ((SystemClock.elapsedRealtime() - mLastLocationMillis) < GPS_UPDATE_TIME_INTERVAL * 2) {
    isGPSFIX = true;
}

工作得像梦一样顺利。有趣,我没想到会这样。谢谢你的帮助。 - Alin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接