Pier-Luc Brault
Pier-Luc Brault

Reputation: 247

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?

Upvotes: 7

Views: 3811

Answers (1)

mikebertiean
mikebertiean

Reputation: 3621

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

Upvotes: 7

Related Questions