I have a GeofenceImplementorClass. I'm adding three geofences in it, one of which is my current location that I got from LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
. I'm expecting the IntentClass service to get executed as I am already at one of the geofence locations however the service is not getting executed. Please help.
public class GeofenceImplementor implements ResultCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private ArrayList<Geofence> geofenceList = new ArrayList<>();
private PendingIntent pendingIntent = null;
private GoogleApiClient googleApiClient;
private Boolean isConnected = false;
private Boolean request = false;
private Context context;
public GeofenceImplementor(Context context){
this.context = context;
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
public void addToList(LatLng latLng,String key){
geofenceList.add(new Geofence.Builder()
.setRequestId(key)
.setCircularRegion(latLng.latitude, latLng.longitude, 1000)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(geofenceList);
return builder.build();
}
private PendingIntent getPendingIntent(){
if(pendingIntent != null)
return pendingIntent;
Intent intent = new Intent(context,IntentClass.class);
return PendingIntent.getService(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
}
public void implement() {
if(isConnected) {
try {
LocationServices.GeofencingApi.addGeofences(googleApiClient,
getGeofencingRequest(), getPendingIntent()).setResultCallback(this);
} catch (SecurityException ex) {
Log.e("Security Exception", ex.getStackTrace().toString());
}
}
else request = true;
}
}
My current location radius was greater than the geofence. Increasing the Geofence circle radius to 3000m solved the issue.