我想知道如何捕捉事件或什么时候我的LocationReqest过期,继承人代码,然后我称之为
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setExpirationDuration(500);
mLocationRequest.setNumUpdates(1);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
现在我需要知道我的LocationRequest是休息,ty求助:)
编辑
我以为我能抓住它
public void onLocationChanged(Location location) {
//something is here
}
但它不起作用:(
EDIT2
我部分解决了它通过添加处理程序检查N + 500ms如果位置设置,我仍然想知道我是否可以没有处理程序
你必须自己处理它。在requestLocationUpdates之后立即发布一个带有延迟的Runnable,如下所示:
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setExpirationDuration(500);
mLocationRequest.setNumUpdates(1);
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mHandler.postDelayed(mExpiredRunnable, 500);
这是Runnable:
private final Runnable mExpiredRunnable = new Runnable() {
@Override
public void run() {
showUnableToObtainLocation();
}
};
当无法获得位置修复时,showUnableToObtainLocation方法将具有您想要执行的任何逻辑。
在您实际获得位置修复的正常情况下,您将代码放在onLocationChanged中以取消Runnable:
mHandler.removeCallbacks(mExpiredRunnable);
您还需要在onPause方法中使用相同的代码,以防在位置修复或请求到期之前将Activity / Fragment置于后台。