安卓GPS状态我逼疯 [英] Android GPS Status driving me nuts

查看:91
本文介绍了安卓GPS状态我逼疯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

阅读在这里后,我发现了一些事件onLocationChanged触发1.6版本,但不晚,所以服用的意见,我实现了一个GPS监听器。我有一些非常怪异的行为作为图标的状态被搅乱了。比如我启用GPS,并得到橙色......经过修复GET的绿色......几秒钟后得到第二橙色后阅读等等...

下面是code我用。请建议改一下

 公共类TrackExecutionActivity延伸活动{

受保护的静态最后长GPS_UPDATE_TIME_INTERVAL = 3000; //米利斯
受保护的静态最终浮动GPS_UPDATE_DISTANCE_INTERVAL = 0; //米
私人LocationManager mlocManager;
私人MyGPSListener mGpsListener;
私人LocationListener的mlocListener;

@覆盖
公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.trackexecution);

        imgGpsState =(ImageView的)findViewById(R.id.imgGpsState);
        mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mlocListener =新MyLocationListener();
        mGpsListener =新MyGPSListener();
}

私有类MyGPSListener实现GpsStatus.Listener {
        公共无效onGpsStatusChanged(INT事件){
            布尔isGPSFix = FALSE;
            开关(事件){
                案例GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    如果(mLastLocation!= NULL)
                        isGPSFix =(SystemClock.elapsedRealtime() -  mLastLocationMillis)所述; GPS_UPDATE_TIME_INTERVAL * 2;

                    如果(isGPSFix){//一个修复已被收购。
                        imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_green));
                    }其他{//此修复程序已丢失。
                        imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_orange));
                    }

                    打破;

                案例GpsStatus.GPS_EVENT_FIRST_FIX:
                    imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_green));
                    打破;
                案例GpsStatus.GPS_EVENT_STARTED:
                    imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_orange));
                    打破;
                案例GpsStatus.GPS_EVENT_STOPPED:
                    imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_red));
                    打破;
            }
        }
    }

公共类MyLocationListener实现LocationListener的
    {
        公共无效onLocationChanged(位置定位)
        {
            如果(位置!= NULL){
                       mLastLocationMillis = SystemClock.elapsedRealtime();
            //做一些事情在这里
                     mLastLocation =位置;

        }
        公共无效onProviderDisabled(字符串提供商)
        {
            imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_red));
        }

        公共无效onProviderEnabled(字符串提供商)
        {
            imgGpsState.setImageDrawable(ctx.getResources()getDrawable(R.drawable.gps_on_orange));
        }

        //这不会触发在Android 2.x的用户说,
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员)
        {
        }
        }
        }

 @覆盖
    保护无效onResume(){
     如果(mlocManager!= NULL){
         如果(mGpsListener == NULL)
         {
            mGpsListener =新MyGPSListener();
         }

         如果(mlocListener == NULL)
         {
            mlocListener =新MyLocationListener();
         }

        mlocManager.addGpsStatusListener(mGpsListener);
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,GPS_UPDATE_TIME_INTERVAL,GPS_UPDATE_DISTANCE_INTERVAL,mlocListener);
      }
       super.onResume();
    }
}
 

解决方案

更​​改此设置:

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

您的设置与非布尔值..所以在GPS定位你需要的地方..设置 isGPSfix 布尔的情况下<$的怪异图标行为布尔C $ C> hasGPSfix 或 doesnothaveGPSfix ..

您可能意味着:

  IF((SystemClock.elapsedRealtime() -  mLastLocationMillis)&LT; GPS_UPDATE_TIME_INTERVAL * 2){
    isGPSFIX = TRUE;
}
 

I am making a application that uses the GPS receiver. The application will work on all versions starting from 1.6. I have a satellite Icon in which I tell the users the current status:

  • if icon is red - gps disabled
  • if icon is orange - gps is enabled and trying to fix on the satellites
  • if icon is green - gps is fixed and running fine.

After reading around here, I've found that some events for onLocationChanged trigger on 1.6 version but not later, so taking the advice I implemented a GPS listener. I have some really weird behavior as the status of the icon gets messed out. For instance I enable GPS and gets orange... after a fix get's green.. after a few seconds gets read after a second orange and so on...

Here is the code I use. Please suggest what to change

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

解决方案

change this:

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

your are setting a boolean with non boolean values.. hence the weird icon behavior during gps fixes you need to set that isGPSfix boolean somewhere.. for case of hasGPSfix or doesnothaveGPSfix..

You might have meant:

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

这篇关于安卓GPS状态我逼疯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆