0

歩行中に GPS からの距離を計算する Android アプリケーションがあります。初めての距離計算は正確なようです。しかし、アプリケーションを再起動すると、最初は以前の距離が表示されますが、0 から始まるはずです。なぜこれが起こっているのでしょうか? これが私のコードです:

public class LocationService extends Service implement LocationListener {

public static final String BROADCAST_ACTION = "com.myapp.services.client.android.LocationService.updated";
public static boolean isPause=false;
Intent intent;
LocationManager locationManager;
Location lastLocation;
boolean distExtra = false;
int sat_count = 0;

@Override
public IBinder onBind(Intent intent) {

    return null;
}


@Override
public void onCreate() {

    intent = new Intent(BROADCAST_ACTION);
    lastLocation = null;

}


@Override
public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(true);
    criteria.setSpeedRequired(true);
    criteria.setCostAllowed(true);
    criteria.setBearingRequired(true);

    locationManager.getBestProvider(criteria,
            false);

    String provider = locationManager.getBestProvider(criteria, true);


    if(intent != null && intent.hasExtra("ExerciseIsDistanceBased")){
        distExtra = intent.getBooleanExtra("ExerciseIsDistanceBased", false);
    }

    if ( intent != null && distExtra ) {

        if ( locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, this);
        }

    }
    else {
        if ( locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, this);
        }

    }

    locationManager.requestLocationUpdates(provider, 0, 5, this);

}


@Override
public void onLocationChanged(Location location) {

    if ( location != null ) {
        if(!isPause){
            intent.putExtra("longitude", location.getLongitude());
            intent.putExtra("latitude", location.getLatitude());
            intent.putExtra("altitude", (float) location.getAltitude());
            float distance = 0.0f;
            if ( lastLocation != null ) {
                distance = location.distanceTo(lastLocation);
            }
            if (location.hasAccuracy()) {
                intent.putExtra("accuracy", (float) location.getAccuracy());
            }
            intent.putExtra("distance", distance);
            lastLocation = location;
            sendBroadcast(intent);
        }
    }
}


@Override
public void onProviderDisabled(String provider) {

    // TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String provider) {

    // TODO Auto-generated method stub
    if((locationManager != null)){
        if(distExtra){
            if ( locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5, this);
            }
        }else{

        }
    }
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
    if(intent != null){
        if(status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE){
            sendBroadcast(intent);
        }
    }
}


@Override
public void onDestroy() {
    if(locationManager != null){
        locationManager.removeUpdates(this);
        locationManager=null;
    }
    lastLocation = null;
    intent=null;

}
}
4

0 に答える 0