我应该用什么来代替它?此外,我针对此地理围栏应用程序的目标是 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
来访问 Geofencing API。只需设置一个 Geofencing 客户端,然后以与之前调用类似的方式调用它。主要区别在于您不必实现结果回调,您可以添加所需的任何成功/失败/完成回调。
所以对于您的代码,它将是...
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;
}
});
请注意,在调用此代码之前,您仍然需要检查您的权限。
关于android - "GeofencingAPI is deprecated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686772/