GeofencingApi is deprecated
It have been replace by "GeofencingClient" but how do you integrate that in the code ?
public void registerAllGeofences(){
if (mGoogleApiClient == null || mGoogleApiClient.isConnected() || mGeofenceList == null || mGeofenceList.size() == 0) {
return;
}
try {
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent()).setResultCallback(this);
} catch (SecurityException securityException) {
Log.e(TAG, securityException.getMessage());
}
}
public void unRegisterAllGeofences() {
if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()) {
return;
}
try {
LocationServices.GeofencingApi.removeGeofences(
mGoogleApiClient,
// This is the same pending intent that was used in registerGeofences
getGeofencePendingIntent()
).setResultCallback(this);
} catch (SecurityException securityException) {
// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
Log.e(TAG, securityException.getMessage());
}
}
Become ?
-public class Geofencing implements ResultCallback {
+public class Geofencing implements OnSuccessListener, OnFailureListener {
+ private GeofencingClient geofencingClient;
//onCreate()
+ geofencingClient = LocationServices.getGeofencingClient(mContext);
//registerAllGeofences()
- LocationServices.GeofencingApi.addGeofences(
- mGoogleApiClient,
- getGeofencingRequest(),
- getGeofencePendingIntent()
- ).setResultCallback(this);
+ geofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
+ .addOnSuccessListener(this)
+ .addOnFailureListener(this);
//unRegisterAllGeofences()
- LocationServices.GeofencingApi.removeGeofences(
- mGoogleApiClient,
+ geofencingClient.removeGeofences(
- ).setResultCallback(this);
+ ).addOnSuccessListener(this)
+ .addOnFailureListener(this)
+ ;
- public void updateGeofencesList(PlaceBuffer places) {
+ public void updateGeofencesList(List<Place> places) {
- if (places == null || places.getCount() == 0) return;
+ if (places == null || places.size() == 0) return;
- public void onResult(@NonNull Result result) {
- Log.e(TAG, String.format("Error adding/removing geofence : %s",
- result.getStatus().toString()));
+ public void onFailure(@NonNull Exception e) {
+ Log.d(TAG, "onFailure: Geofence add failure");
+ @Override
+ public void onSuccess(Object o) {
+ Log.d(TAG, "onSuccess: Geofence adeed");
+ }