Android LocationRequest:请求过期时获取回调 [英] Android LocationRequest: get a callback when request expires

查看:525
本文介绍了Android LocationRequest:请求过期时获取回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何捕获事件,或者当LocationReqest过期时会发生什么,这就是我称之为的代码

im wonder how to catch event or what ever when my LocationReqest expired, heres code then i call it

mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setExpirationDuration(500);
    mLocationRequest.setNumUpdates(1); 
    mLocationClient.requestLocationUpdates(mLocationRequest, this);

现在我需要知道我的LocationRequest坏了,请帮忙:)

now i need to get know that my LocationRequest was break, ty for help :)

编辑

我认为我可以抓住它

public void onLocationChanged(Location location) {
//something is here
}

但是它不起作用:(

edit2

我通过添加处理程序来部分解决它,该处理程序会在N + 500毫秒后检查是否设置了位置,我仍然想知道我是否可以在没有处理程序的情况下做到这一点

i partial solved it by adding handler that check after N+500ms if location was set, im still wonder if i can do it without handler

推荐答案

您必须自己处理.像这样在requestLocationUpdate之后立即延迟发布Runnable:

You'll have to handle it yourself. Post a Runnable with a delay immediately after requestLocationUpdates like this:

mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setExpirationDuration(500);
mLocationRequest.setNumUpdates(1); 
mLocationClient.requestLocationUpdates(mLocationRequest, this);
mHandler.postDelayed(mExpiredRunnable, 500);

这是Runnable:

Here's the Runnable:

private final Runnable mExpiredRunnable = new Runnable() {
    @Override
    public void run() {
        showUnableToObtainLocation();
    }
};

当无法获得位置定位信息时,showUnableToObtainLocation方法将具有您要执行的逻辑.

The showUnableToObtainLocation method would have whatever logic you wanted to execute when a location fix could not be obtained.

通常情况下,您确实会获得位置修复,您可以将代码放在onLocationChanged中以取消Runnable:

In the normal case where you actually do get a location fix you put code in onLocationChanged to cancel the Runnable:

mHandler.removeCallbacks(mExpiredRunnable);

如果在位置修复或请求过期之前活动/片段"已后台运行,您还希望在onPause方法中使用相同的代码.

You would also want this same code in your onPause method as well in case the Activity/Fragment is backgrounded before a location fix OR the request expires.

这篇关于Android LocationRequest:请求过期时获取回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆