地理围栏API已弃用。

3

我该用什么来替换它?此外,我正在为这个地理围栏应用程序针对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);
    }

}

使用 GeofencingClient - Neria Nachum
1个回答

5
您正在使用的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;
                        }
                    });

请注意,在调用此代码之前,您仍需要检查您的权限。
有关更详细的解释,请参见此处此处

2
我已经使用geoFencingClient工作了一段时间。Google的示例没有说明在onFailure()中要做什么,但是我通过实际经验知道有时会无法添加地理围栏。为什么?我知道限制是100个,但即使只是第二次添加,它也可能失败。为什么? - Someone Somewhere

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接