【发布时间】: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();
}
}
【问题讨论】: