Search code examples
androidgoogle-play-servicesfusedlocationproviderapi

How to change FusedLocationApi update interval after requestLocationUpdates has been called


I am using LocationServices.FusedLocationApi to get location updates at a given interval, which is set on the LocationRequest object that is passed to requestLocationUpdates:

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(30000);
LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

I need to change the value of the interval after the listener started to receive location updates. What is the proper way to do that?


Solution

  •     //remove location updates so that it resets
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    
        //change the time of location updates
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(TIME_INTERVAL)
                .setFastestInterval(TIME_INTERVAL_MIN);
    
        //restart location updates with the new interval
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);