android - "GeofencingAPI is deprecated"

标签 android android-geofence

我应该用什么来代替它?此外,我针对此地理围栏应用程序的目标是 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;
                        }
                    });

请注意,在调用此代码之前,您仍然需要检查您的权限。

参见 herehere以获得更全面的解释。

关于android - "GeofencingAPI is deprecated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686772/

相关文章:

android - 如何删除我在 Android Q 中创建的文件?

android - 尝试在Unity3d中编译 Assets 之一时编译项目卡纸

android - 动画文本更改

android - 在 Android 中进入 GeoFence 时无法显示通知

当有人进入或离开某个区域时,Android 会收到通知

android - 无法连接到 Google API 客户端

android - 在android中的sdcard中创建一个只读文件夹

android - 如何增加 FCM 推送通知文本的字体大小

android - 检查圆形地理围栏内的地理位置(纬度,经度)

android - 如何让地理围栏精准?