Android LocationRequest: get a callback when request expires

删除回忆录丶 提交于 2019-11-30 17:33:48

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);

Here's the Runnable:

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

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

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

mHandler.removeCallbacks(mExpiredRunnable);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!