android - How to stop requestLocationUpdates() after a certain amount of time? -


in android application use googleapiclient work location service. works when call requestlocationupdates() in following way:

locationprovider.requestlocationupdates(mgoogleapiclient, locationrequest, (locationlistener) thisfragment); 

where mgoogleapiclient initialized in oncreate() method of activity:

private googleapiclient mgoogleapiclient; [...]  // oncreate() method in activity mgoogleapiclient = new googleapiclient.builder(this)     .addapi(plus.api, plus.plusoptions.builder().build())     .addapi(locationservices.api)     .addscope(new scope("email"))     .addconnectioncallbacks(this)     .addonconnectionfailedlistener(this)     .build(); 

while locationprovider , locationrequest initialized in onattach() method of fragment implements com.google.android.gms.location.locationlistener:

//onattach() method in fragment this.locationprovider = locationservices.fusedlocationapi;  this.locationrequest = new locationrequest(); this.locationrequest     .setinterval(constants.google_location_interval)     .setfastestinterval(constants.google_fastest_location_interval)     .setpriority(locationrequest.priority_high_accuracy); 

the problem that, sometimes, user can ask retrieving location in indoor environment, i'd terminate requestlocationupdates() request after amount of time.

so far, i've tried following solutions, without success:

1) using looper , handler. solution worked old locationmanager.

looper looper = looper.mylooper(); handler handler = new handler(looper);  handler.postdelayed(new runnable() { @override     public void run() {         locationprovider.requestlocationupdates(mgoogleapiclient, locationrequest, (locationlistener) thisfragment, looper);     } }, constants.location_timeout_ms); 

2) using handler

handler handler = new handler();  handler.postdelayed(new runnable() { @override     public void run() {         locationprovider.requestlocationupdates(mgoogleapiclient, locationrequest, (locationlistener) thisfragment);     } }, constants.location_timeout_ms); 

however, in both 2 cases, timeout (expressed constants.location_timeout_ms) never expires. gps service keeps working endlessly.

first of 2 codes identical:

looper looper = looper.mylooper(); handler handler = new handler(looper); 

and

handler handler = new handler(); 

if check source code can see empty constructor handler internally uses looper.mylooper();

i'm not sure think cancelling request, after time passes, calling requestlocationupdates again. yes, keep trying gps lock.

i'll suggest 2 methods how should doing.

  1. this first 1 easier i'm not sure how works, used passive location updates. use expiration on request, after expirated time, location services should quit automatically.

    locationrequest.setexpirationduration(constants.location_timeout_ms); 
  2. this second use correct method, removelocationupdates

    locationprovider.removelocationupdates(              mgoogleapiclient,               (locationlistener) thisfragment); 

Comments