Skip to content Skip to sidebar Skip to footer

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(Lo

Solution 1:

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:

privatefinalRunnablemExpiredRunnable=newRunnable() {
    @Overridepublicvoidrun() {
        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.

Post a Comment for "Android Locationrequest: Get A Callback When Request Expires"