Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"GeofencingAPI is deprecated"

What should I replace this with? Also, I am targeting Android 7.0 for this Geofence App.

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);
    }

}
like image 269
captain_watanabe Avatar asked Jul 03 '26 20:07

captain_watanabe


1 Answers

The version of Android you are using is not related to the use of the deprecated GeofencingApi. The GeofencingApi is part of Google Play Services and was deprecated in release 11.0.

At this time the alternative GeofencingClient was added to Google Play Services.

You therefore no longer need to set up a GoogleApiClient in order to access the Geofencing API. Just set up a Geofencing client, then call it in a similar way to your previous call. The main difference is that you do not have to implement a result callback, you can add whichever success/failure/completed callback you require.

So for your code it would be...

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;
                        }
                    });

Note you would still need to check your permissions before calling this code.

See here and here for a fuller explanation.

like image 89
Simon Hutton Avatar answered Jul 06 '26 10:07

Simon Hutton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!