Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android LocationRequest: get a callback when request expires

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

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

edit

i thought i could catch it in

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

but its doesnt work :(

edit2

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

like image 901
Mariusz Avatar asked Mar 14 '14 20:03

Mariusz


1 Answers

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.

like image 150
Matt Accola Avatar answered Oct 20 '22 15:10

Matt Accola