0

So far online I can only find way to set the expiration of a geofence with time in millis. I would like to have a user arrive at a geofence, exit it and then the geofence would expire. but I can only find how to either set a time or set it to never expire.

.setExpirationDuration( 1000000 )

Is there no constant variable for this? Maybe it has to be done by deleting the geofence in the intent that handles it?

Thanks

AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68

1 Answers1

1

If you only care about setting the timeout upon an exit event, just update that geofence once it triggers an exit. This is a very basic example of how to do this, with notes on how to improve it. Once you get this working, you could essentially have a keep alive for geofences, only timing out when they haven't been triggered in some time.

public void onReceive(Intent intent) {
    //.... your code ....

    GeofencingEvent event = GeofencingEvent.fromIntent(intent);

    if (event.getGeofenceTransition() == GEOFENCE_TRANSITION_EXIT) {
        //This assumes you only worry about one geofence, but there may be others in this list...
        List<Geofence> geofences = event.getTriggeringGeofences();
        Geofence geofence = geofences.get(0);
        String id = geofence.getRequestId(); //Do a check to see if this is the geofence you want to modify.

        //Either send the id to the class that handles adding geofences, or have a reference to your GoogleApiClient here.

        //Assuming you have GoogleApiClient reference here...
        Geofence.Builder geofenceBuilder = new Geofence.Builder();
        geofenceBuilder.setCircularRegion(lat, lon, radius);
        geofenceBuilder.setExpiration(1000000);
        geofenceBuilder.setRequestId(id);
        geofenceBuilder.setTransitionTypes(transitionTypes);

        //The GeofencingRequest class is parcelable too, so this be another option to send your request to a class that handles google api calls.
        GeofencingRequest.Builder requestBuilder = new GeofencingRequest.Builder();
        requestBuilder.addGeofence(geofenceBuilder.build());
        requestBuilder.setInitialTrigger(initialTrigger);
        LocationServices.GeofencingApi.addGeofences(apiClient, requestBuilder.build(), pendingIntent);
    }

    //... the rest of your stuff
}
Pablo Baxter
  • 2,144
  • 1
  • 17
  • 36