如何准确修复 requestLocationUpdates 的弃用警告。
Android LocationServices.FusedLocationApi deprecated 有答案。但我不知道如何解决
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(DriverMapActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_REQUEST_CODE );
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
答案 0 :(得分:0)
FusedLocationProviderApi
已被弃用很长时间,这就是为什么官方指南建议使用FusedLocationProviderClient
的原因。只需致电startLocationUpdates()
即可获得如下更新:
override fun onResume() {
super.onResume()
if (requestingLocationUpdates) startLocationUpdates()
}
private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback,
null /* Looper */)
}
答案 1 :(得分:0)
使用FusedLocationProviderClient类。这是Google希望我们使用的一种。
我正在为您提供一个简短的示例。
private FusedLocationProviderClient mFusedLocationClient;
private LocationRequest locationRequest;
private LocationCallbackImpl mLocationCallback = null;
...
if (mFusedLocationClient == null) {
locationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(1000)
.setFastestInterval(1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locationRequest);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity);
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());
}
/**
* Callback class
*/
private class LocationCallbackImpl extends LocationCallback {
private Activity activity;
public LocationCallbackImpl(Activity activity) {
this.activity = activity;
}
@Override
public void onLocationResult(LocationResult locationResult) {
}
}
答案 2 :(得分:0)
您需要使用doc.Save(filePath);
}
FusedLocationProverClient
在您的
private FusedLocationProviderClient fusedLocationClient;
方法中初始化onCreate
fusedLocationClient
请求位置更新
fusedLocationClient = LocationServices.getFusedLocationProviderClient(Activity.this);
希望有帮助!