不推荐使用LocationServices.SettingsApi [英] LocationServices.SettingsApi deprecated

查看:275
本文介绍了不推荐使用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已被弃用.如何用新的代码替换不推荐使用的代码?

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

我在阅读文档时发现解决方案可以是使用SettingsClient,但无法弄清楚该怎么做.

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?

推荐答案

LocationServices.SettingsApi已弃用

LocationServices.SettingsApi deprecated

是, LocationServices.SettingsApi 已弃用

如何用新代码替换不推荐使用的代码?

How can I replace deprecated code with the new one?

您需要使用 GoogleApi-基于API的设置客户端

You need to use GoogleApi-based API SettingsClient

从DOCS

SettingsClient

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

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

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

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

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.

在请求位置服务时,设备的系统设置可能处于阻止应用程序获取其所需位置数据的状态.例如,可以关闭GPS或Wi-Fi扫描.此意图使您轻松进行以下操作:

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.

我在阅读文档时发现解决方案可以是使用SettingsClient,但无法弄清楚该怎么做.

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

按照此步骤

要使用此API,请先创建一个LocationSettingsRequest.Builder并添加应用将使用的所有LocationRequests:

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

这篇关于不推荐使用LocationServices.SettingsApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆