Search code examples
androidgpslocationclient

SettingsApi deprecated


Until last update I used next code:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        builder.setAlwaysShow(true);
        mLocationSettingsRequest = builder.build();

        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(
                        mGoogleApiClient,
                        mLocationSettingsRequest
                );
        result.setResultCallback(this);

Unfortunately after update a warning message pup-up that LocationServices.SettingsApi is deprecated. How to change my code to fit to new updates?


Solution

  • SettingsApi interface is Deprecated. You should use GoogleApi-based API SettingsClient instead.

    public class SettingsClient extends GoogleApi

    This API makes it easy for an app to ensure that the device's system settings are properly configured for the app's location needs.

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest); // mLocationRequest is a Object of LocationRequest
        LocationSettingsRequest locationSettingsRequest = builder.build();
    
        SettingsClient settingsClient = LocationServices.getSettingsClient(this);
        settingsClient.checkLocationSettings(locationSettingsRequest);