计算 onLocationChanged 方法之间的时间
Posted
技术标签:
【中文标题】计算 onLocationChanged 方法之间的时间【英文标题】:Calculate time between onLocationChanged method 【发布时间】:2017-08-24 22:13:21 【问题描述】:我需要计算每次位置变化(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 方法时的时间差。
【问题讨论】:
【参考方案1】:正确的方法?只需使用 Location 对象的getTime() 方法即可。
比较新的和以前的位置,然后找出差异。
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;
【讨论】:
以上是关于计算 onLocationChanged 方法之间的时间的主要内容,如果未能解决你的问题,请参考以下文章
如何在 onMapReady 方法中访问我在 onLocationChanged 中的当前位置?
为啥在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法?