微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐