android – LocationServices.SettingsApi已弃用

我的代码是:

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

    }

但不幸的是,不推荐使用LocationServices.SettingsApi.如何用新的代码替换已弃用的代码

我找到了阅读文档,解决方案可以是使用SettingsClient,但无法弄清楚如何做到这一点.

有什么想法可以更新我的代码吗?

解决方法:

LocationServices.SettingsApi deprecated

是的,LocationServices.SettingsApi已被弃用

How can I replace deprecated code with the new one?

你需要使用GoogleApi-based API SettingsClient

来自DOCS

SettingsClient

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

与位置设置启用程序API交互的主要入口点.

此API使应用程序可以轻松确保为应用程序的位置需求正确配置设备的系统设置.

在向位置服务发出请求时,设备的系统设置可能处于阻止应用程序获取其所需位置数据的状态.例如,可以关闭GPS或Wi-Fi扫描.这个意图使它很容易:

>确定是否在设备上启用了相关的系统设置以执行所需的位置请求.
>(可选)调用一个对话框,允许用户通过单击启用必要的位置设置.

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

按照以下步骤操作

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...