captain_watanabe
captain_watanabe

Reputation: 119

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

}

Upvotes: 3

Views: 3526

Answers (1)

Simon Hutton
Simon Hutton

Reputation: 1777

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.

Upvotes: 5

Related Questions