我的代码是:
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());