我有一个Android应用程序,可以计算行走时与GPS的距离。第一次距离计算似乎准确。但是当我重新启动应用程序时,它最初会显示之前的距离,但它应该从0开始。任何想法为什么会发生这种情况?这是我的代码:
public class LocationService extends Service实现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;
}
}