Android 计算onLocationChanged方法之间的时间间隔

Android 计算onLocationChanged方法之间的时间间隔,android,geolocation,location,android-location,android-fusedlocation,Android,Geolocation,Location,Android Location,Android Fusedlocation,我需要计算每个位置更改(onLocationChanged)之间的时间差。这样做的正确方法是什么 获取用户位置的我的代码: public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private Looper mServiceLoop

我需要计算每个位置更改(onLocationChanged)之间的时间差。这样做的正确方法是什么

获取用户位置的我的代码:

public class LocationService extends Service implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private DatabaseHelpers mDatabaseHelpers;

private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        super(looper);
    }
}

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
    mDatabaseHelpers = new DatabaseHelpers(LocationService.this);
}

public void buildGoogleApiClient() {
    createLocationRequest();
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private String getCurrentTimeStamp() {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm");
    String format = simpleDateFormat.format(new Date());
    return format;
}

public void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
}

protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
    mGoogleApiClient.disconnect();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("Location Service", "Started");
    buildGoogleApiClient();
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    // The service is no longer used and is being destroyed
    stopLocationUpdates();
}

@Override
public void onLocationChanged(Location location) {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    startLocationUpdates();
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
在上面的代码中,我得到了用户的位置,但我需要计算调用onLocationChanged方法时的时间差。

正确的方法?只需使用定位对象的方法

比较新位置和以前的位置,然后找出差异

private Location mPrevLocation;

@Override
public void onLocationChanged(Location location) {
    if(mPrevLocation != null){
        long prevTime = mPrevLocation.getTime();
        long currentTime = location.getTime();

        // This is exactly what you want 
        long diffTime = currentTime - prevTime ;
    } 

    mPrevLocation = location;
}