LocationServices.SettingsApi 已弃用
Posted
技术标签:
【中文标题】LocationServices.SettingsApi 已弃用【英文标题】:LocationServices.SettingsApi deprecated 【发布时间】:2018-09-13 20:11:23 【问题描述】:我的代码是:
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,但不知道该怎么做。
有什么办法可以更新我的代码吗?
【问题讨论】:
【参考方案1】:LocationServices.SettingsApi 已弃用
是的,LocationServices.SettingsApi
已弃用
如何用新代码替换已弃用的代码?
你需要使用GoogleApi-based API SettingsClient
来自文档
设置客户端
public class SettingsClient extends GoogleApi<Api.ApiOptions.NoOptions>
与位置设置启用 API 交互的主要入口点。
此 API 可让应用轻松确保设备的系统设置针对应用的位置需求进行了正确配置。
向位置服务发出请求时,设备的系统设置可能会阻止应用获取所需的位置数据。例如,GPS 或 Wi-Fi 扫描可能会关闭。这种意图很容易:
确定设备上是否启用了相关系统设置以执行所需的位置请求。
(可选)调用一个对话框,允许用户通过单击启用必要的位置设置。
我在阅读文档时发现可以使用 SettingsClient 解决方案,但不知道该怎么做。
按照这些步骤进行
要使用此 API,首先创建一个 LocationSettingsRequest.Builder 并添加应用将使用的所有 LocationRequest:
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestHighAccuracy)
.addLocationRequest(mLocationRequestBalancedPowerAccuracy)
然后检查当前位置设置是否满足:
Task<LocationSettingsResponse> result =
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
【讨论】:
【参考方案2】:新代码是
protected void createLocationRequest()
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>()
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse)
Toast.makeText(MapActivity.this, "addOnSuccessListener", Toast.LENGTH_SHORT).show();
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
);
task.addOnFailureListener(this, new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Toast.makeText(MapActivity.this, "addOnFailureListener", Toast.LENGTH_SHORT).show();
if (e instanceof ResolvableApiException)
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MapActivity.this,
REQUEST_CHECK_SETTINGS);
catch (IntentSender.SendIntentException sendEx)
// Ignore the error.
);
【讨论】:
【参考方案3】:我在弃用LocationServices.SettingsApi
和FusedLocationApi
后使用此代码
此代码提供:
1)请求最后的位置
2) 使用FusedLocationProviderClient
定期更新位置
您的活动中的所有代码
//create this at top of onCreate
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//...
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//ADD THIS LINE
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
buildGoogleApiClient();
createLocationRequest();
Loc_Update();
//...
protected synchronized void buildGoogleApiClient()
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks()
@Override
public void onConnected(@Nullable Bundle bundle)
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>()
@Override
public void onSuccess(Location location)
if (location!=null)
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
);
@Override
public void onConnectionSuspended(int i)
//LOG
)
.addApi(LocationServices.API)
.build();
protected void createLocationRequest()
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(30000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
private void Loc_Update()
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>()
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task)
try
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
if (ContextCompat.checkSelfPermission(MainMapActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
mFusedLocationClient.requestLocationUpdates(mLocationRequest,new LocationCallback()
@Override
public void onLocationResult(LocationResult locationResult)
for (Location location : locationResult.getLocations())
//Do what you want with location
//like update camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16f));
,null);
catch (ApiException exception)
switch (exception.getStatusCode())
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the
// user a dialog.
try
// Cast to a resolvable exception.
ResolvableApiException resolvable = (ResolvableApiException) exception;
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
resolvable.startResolutionForResult(MainMapActivity.this, 2001);
break;
catch (IntentSender.SendIntentException e)
// Ignore the error.
catch (ClassCastException e)
// Ignore, should be an impossible error.
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
);
【讨论】:
【参考方案4】:使用 SettingsClient 代替 SettingsApi 阅读官方文档-enter link description here
【讨论】:
以上是关于LocationServices.SettingsApi 已弃用的主要内容,如果未能解决你的问题,请参考以下文章