Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

android - LocationServices.SettingsApi deprecated

My code is:

if (mGoogleApiClient == null && checkGooglePlayService()) {
        Log.d(Utils.TAG_DEV + TAG, "Building GoogleApiClient");
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        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);

    }

but unfortunately the LocationServices.SettingsApi is deprecated. How can I replace deprecated code with the new one?

I found reading docs that the solution can be to use SettingsClient but couldn't figure how to do it.

Any ideea what to do to can update my code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

LocationServices.SettingsApi deprecated

Yes, LocationServices.SettingsApi is deprecated

How can I replace deprecated code with the new one?

You need to use GoogleApi-based API SettingsClient

FROM DOCS

SettingsClient

public class SettingsClient extends GoogleApi<Api.ApiOptions.NoOptions>

The main entry point for interacting with the location settings-enabler APIs.

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.

When making a request to location services, the device's system settings may be in a state that prevents an app from obtaining the location data that it needs. For example, GPS or Wi-Fi scanning may be switched off. This intent makes it easy to:

  • Determine if the relevant system settings are enabled on the device to carry out the desired location request.

  • Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.

I found reading docs that the solution can be to use SettingsClient but couldn't figure how to do it.

Follow this steps

To use this API, first create a LocationSettingsRequest.Builder and add all of the LocationRequests that the app will be using:

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
     .addLocationRequest(mLocationRequestHighAccuracy)
     .addLocationRequest(mLocationRequestBalancedPowerAccuracy)

Then check whether current location settings are satisfied:

Task<LocationSettingsResponse> result =
         LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...