问题 Android LocationRequest:请求到期时获取回调


我想知道如何捕捉事件或什么时候我的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如果位置设置,我仍然想知道我是否可以没有处理程序


12628
2018-03-14 20:08


起源

该 文件 在这种情况下我不会谈论任何回调,如果您的请求过期而没有任何位置更新,我猜您必须使用计时器来触发事件。 - Stéphane


答案:


你必须自己处理它。在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置于后台。


13
2018-04-30 05:07