Android &引用;GeofencingAPI已被弃用;
我应该用什么来代替这个?此外,我还将安卓7.0作为这个Geofence应用程序的目标Android &引用;GeofencingAPI已被弃用;,android,android-geofence,Android,Android Geofence,我应该用什么来代替这个?此外,我还将安卓7.0作为这个Geofence应用程序的目标 private void addNewGeofence(GeofencingRequest request) { Log.i(TAG, "GEOFENCE: Adding new Geofence."); if (checkPermissions()){ if (ActivityCompat.checkSelfPermission(this, Manifest.permissio
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版中被弃用
此时,Google Play服务中添加了替代的GeofencingClient
因此,您不再需要设置GoogleAppClient
来访问地理围栏API。只需设置一个GeoFenging客户端,然后以与上一次调用类似的方式调用它。主要区别在于您不必实现结果回调,您可以添加所需的成功/失败/完成的回调
所以对于你的代码,它将是
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;
}
});
client=LocationServices.getGeofencingClient;
...
addGeofences(请求,createGeoFencePendingEvent())
.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公开作废(作废避免){
//你的成功代码
}
})
.addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
//你的失败代码;
}
});
注意:在调用此代码之前,您仍然需要检查您的权限
有关更全面的解释,请参见和。使用
GeofencingClient
。我已经使用GeofencingClient一段时间了。谷歌的例子并不费心说明在onFailure()中应该做什么,但我从实际经验中知道,它有时确实无法添加地理围栏。为什么?我知道上限是100,但即使只是第二次添加,它也可能失败。为什么?