我应该用什么来代替这个?另外,我的目标是Android 7.0这个地理围栏应用程序。
private void addNewGeofence(GeofencingRequest request) {
Log.i(TAG, "GEOFENCE: Adding new Geofence.");
if (checkPermissions()){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
LocationServices.GeofencingApi.addGeofences(
googleApiClient, request, createGeofencePendingIntent()).setResultCallback(this);
}
}
最佳答案
您使用的android版本与不推荐使用的GeofencingApi
无关。GeofencingApi
是google play服务的一部分,在11.0版中被弃用。
此时,替代的GeofencingClient
被添加到google play服务中。
因此,您不再需要设置GoogleApiClient
来访问地理围栏api。只需设置一个地理围栏客户机,然后以与上次调用类似的方式调用它。主要区别在于不必实现结果回调,可以添加所需的成功/失败/完成回调。
所以对于你的代码来说…
client = LocationServices.getGeofencingClient;
...
client.addGeofences(request, createGeofencePendingIntent())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// your success code
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// your fail code;
}
});
注意,在调用此代码之前,仍需要检查您的权限。
请参见here和here以获得更全面的解释。
关于android - “GeofencingAPI已弃用”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48686772/