使用gps计算速度后应用程序会在一段时间后挂起?

时间:2013-03-19 09:12:27

标签: android geolocation gps

我正在开发具有速度表功能的应用程序。它工作正常大约15-20分钟,但之后应用程序挂起并冻结。会是什么原因?

我为一个名为GPSManager的GPS数据编写了一个单独的类,我从中调用了一个方法onGPSUpdate(),该方法在MainActivity中被覆盖。在该方法中,完成了对速度和位置的所有计算。

以下是我的代码:

public class MainActivity extends ActionBarActivity implements    OnPageChangeListener,GPSCallback,OnClickListener 
{

public void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        gpsManager = new GPSManager();
        gpsManager.startListening(getApplicationContext());
        gpsManager.setGPSCallback(this);
     }
    public void onGPSUpdate(Location location) 
    {
        String directionString = "NORTH";
        float speedDigital = 0 ;

          if (location.hasSpeed())
          {
            speedDigital = location.getSpeed();

            final String speedString = "" + roundDecimal(convertSpeed(speedDigital),2);
            setSpeedText(R.id.textViewSpeed,speedString);
            if (location.hasBearing())
            {

              int bearingAngle = (int) location.getBearing();
              if ((bearingAngle > 337.5D) || (bearingAngle <= 22.5D))
                  directionString = "NORTH " + bearingAngle;
              if ((bearingAngle > 22.5D) && (bearingAngle <= 67.5D))
                  directionString = "N-EAST  "+ bearingAngle;
              if ((bearingAngle > 67.5D) && (bearingAngle <= 112.5D))
                  directionString = "EAST "+ bearingAngle;
              if ((bearingAngle > 112.5D) && (bearingAngle <= 157.5D))
                  directionString = "S-EAST "+ bearingAngle;
              if ((bearingAngle > 157.5D) && (bearingAngle <= 202.5D))
                  directionString = "SOUTH "+ bearingAngle;
              if ((bearingAngle > 202.5D) && (bearingAngle <= 247.5D))
                  directionString = "S-WEST "+ bearingAngle;
              if ((bearingAngle > 247.5D) && (bearingAngle <= 292.5D))
                  directionString = "WEST "+ bearingAngle;
              if ((bearingAngle > 292.5D) && (bearingAngle <= 337.5D))
                  directionString = "N-WEST "+ bearingAngle;
            }
            textViewCompass.setText(directionString);
          }
          else{
              setSpeedText(R.id.textViewSpeed, "0.0");
          }
            Geocoder gcd = new Geocoder(this, Locale.getDefault());
            List<Address> addresses = null;
            try {
                        textViewLocation.setText(currentLocation);
                        textViewLocation.setTextColor(Color.WHITE);
                         }
            } 
            catch (IOException e){
                e.printStackTrace();
            }
     }
         private void setSpeedText(int textid,String text){
        Double val = Double.parseDouble(text);
        Integer intVal = val.intValue(); 
        TextView tv = ((TextView)findViewById(textid));
        tv.setText(""+intVal);
    }
    }
    public class GPSManager{
          private static final int gpsMinTime = 1000;
          private static final int gpsMinDistance = 0; 
        private static LocationManager locationManager = null;
        private static LocationListener locationListener = null;
        private static GPSCallback gpsCallback = null;

        public GPSManager(){       
                GPSManager.locationListener = new LocationListener(){
                        @Override
                        public void onLocationChanged(final Location location) {        
                            if(GPSManager.gpsCallback != null){ 
                                GPSManager.gpsCallback.onGPSUpdate(location);
                            }
                        }

                        @Override
                        public void onProviderDisabled(final String provider){
                            Log.v("gps provider disabled ", provider);
                        }

                        @Override
                        public void onProviderEnabled(final String provider){
                            Log.v("gps provider enabled",provider);
                        }

                        @Override
                        public void onStatusChanged(final String provider, final int status, final Bundle extras){
                            Log.v("gps provider ststus", ""+status);
                        }
                };
        }
        public GPSCallback getGPSCallback() {
                return GPSManager.gpsCallback;
        }

        public void setGPSCallback(final GPSCallback gpsCallback){
                GPSManager.gpsCallback = gpsCallback;
        }

        public void startListening(final Context context) {
                if (GPSManager.locationManager == null){
                        GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                }
            GPSManager.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0F, GPSManager.locationListener); 
            };

        public void stopListening() {
                try {
                        if (GPSManager.locationManager != null && GPSManager.locationListener != null) {
                                GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
                        }
                        GPSManager.locationManager = null;
                }
                catch (final Exception ex){
                }
        }}

0 个答案:

没有答案