I am using geofencing in my android app by following this link .
I create and add a geo object in my default visible activity and listen to its events using the IntentService, as described in the link above.
The Geofence object was successfully added, but when I tested it using a real device and moved it to / from a specific geofence area, its ENTER / EXIT event does not fire.
I tested it with a radius of 100 m, 50 m, 500 m, 1500 m and 2000 m, but it does not work in any of the cases.
This is how I created and added geofencing:
mGeofencingClient = LocationServices.getGeofencingClient(DashboardActivity.this);
Geofence geofence = new Geofence.Builder()
.setRequestId(REQUEST_ID)
.setCircularRegion(
22.703617,
75.873409,
Constants.GEOFENCE_RADIUS_IN_METERS
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT )
.build();
mGeofenceList.add(geofence);
GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER|GeofencingRequest.INITIAL_TRIGGER_EXIT)
.addGeofences(mGeofenceList).build();
Intent intents = new Intent(this, GeofenceTransitionsIntentService.class);
mGeofencePendingIntent = PendingIntent.getService(DashboardActivity.this, 0, intents, PendingIntent.
FLAG_UPDATE_CURRENT);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}else {
mGeofencingClient.addGeofences(geofencingRequest, mGeofencePendingIntent)
.addOnSuccessListener(this, new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.e("geofencing success", "successfully added");
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("geofencing failure", "failed in added");
}
});
}
Run codeHide resultTo listen for ENTER / EXIT events, I use an IntentService that looks like this:
public class GeofenceTransitionsIntentService extends IntentService {
public GeofenceTransitionsIntentService(String name) {
super(name);
}
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
Toast.makeText(this,"service get start",Toast.LENGTH_LONG).show();
Log.e("service","geofencing service started");
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Toast.makeText(this,"error_in_geofancing"+errorMessage,Toast.LENGTH_LONG).show();
return;
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
Toast.makeText(this,"you are inside range",Toast.LENGTH_LONG).show();
String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences);
sendNotification(geofenceTransitionDetails);
Log.e("geofancing","you are inside range");
} else {
Toast.makeText(this,"Invalid_Transition",Toast.LENGTH_LONG).show();
}
}
private String getGeofenceTransitionDetails(
int geofenceTransition,
List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);
ArrayList <String>triggeringGeofencesIdsList = new ArrayList<>();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}
String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);
return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}
}
Run codeHide resultACCESS_FINE_LOCATION LOCATION_HARDWARE .