I am monitoring for geofence transition using the code below
LocationServices.GeofencingApi
.addGeofences(AssistApplication.getApiHelper().getClient(),
getGeofencingRequest(),
getPendingIntent(context,
GeofenceTransitionIntentService.class))
.setResultCallback(this);
This is how I build the GeofencingRequest
private GeofencingRequest getGeofencingRequest()
{
return new GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |
GeofencingRequest.INITIAL_TRIGGER_EXIT)
.addGeofence(getGeofence())
.build();
}
private Geofence getGeofence()
{
return new Geofence.Builder()
.setRequestId(requestId)
.setCircularRegion(latitude, longitude, 100)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
}
The Geofence triggers correctly on enter and exit, but when I use getGeofenceTransition()
, I am not getting any of the three GEOFENCE_TRANSITION_
flags.
protected void onHandleIntent(Intent intent)
{
final GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event.hasError())
{
Log.e(TAG, getErrorMessage(event.getErrorCode()));
return;
}
switch (event.getGeofenceTransition())
{
case Geofence.GEOFENCE_TRANSITION_EXIT:
// Not triggered
break;
case Geofence.GEOFENCE_TRANSITION_ENTER:
case Geofence.GEOFENCE_TRANSITION_DWELL:
// Not triggered
break;
default:
// Triggered on exit and enter
}
}
please advise on what I am missing here
From the information in the question, I can't see any specific problems with your code, but I suspect the
Intent
you are processing isn't from a transition alert?Personally I use a broadcast receiver to receive the
Intent
from the Geofence, with anIntentFilter
used to filter it. The structure I use is as follows :-Declare broadcast receiver in your
Activity
Register this receiver in your
Activity
onCreate
method :-On creation of a broadcast
PendingIntent
, set filter (mGeofencePendingIntent is a memberPendingIntent
variable) :-Monitor your geofences :-
I hope this helps.