Home » Android » android – what is the purpose of `setInitialTrigger` on GeofencingRequest?

android – what is the purpose of `setInitialTrigger` on GeofencingRequest?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I really don’t understand the function setInitialTrigger on GeofencingRequest class.

I know that we can create some Geofence object whith different transition:

  1. Enter
  2. Exit
  3. Dwell

Which is fine for me and acceptable.

Now, my problem is relatively to the class GeofencingRequest and more precisely the method setInitialTrigger.
I really don’t understand the value that we should put there…
Android documentation (here) isn’t really helpful regarding to the meaning of that method.

Imagine that I have this piece of code:

private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(mGeofenceList);
        return builder.build();
    }

What is the meaning of GeofencingRequest.INITIAL_TRIGGER_ENTER ?

For me it means, that GeofencingRequest should trigger any Geofence object which have an ENTER transition.

But imagine that I have multiple Geofence with different behaviour ENTER or EXIT transition.

How should I handle/implement with GeofencingRequest Builder?

How to&Answers:

builder.setInitialTrigge Sets the geofence notification behavior at the moment when the geofences are added.

You can use 3 constants:

public static final int INITIAL_TRIGGER_DWELL

A flag indicating that geofencing service should trigger GEOFENCE_TRANSITION_DWELL notification at the moment when the geofence is added and if the device is already inside that geofence for some time.

Constant Value: 4

public static final int INITIAL_TRIGGER_ENTER

A flag indicating that geofencing service should trigger GEOFENCE_TRANSITION_ENTER notification at the moment when the geofence is added and if the device is already inside that geofence.

Constant Value: 1

public static final int INITIAL_TRIGGER_EXIT

A flag indicating that geofencing service should trigger GEOFENCE_TRANSITION_EXIT notification at the moment when the geofence is added and if the device is already outside that geofence.

Constant Value: 2

What is the meaning of GeofencingRequest.INITIAL_TRIGGER_ENTER ?
INdicate that geofencing service should trigger at the moment when the geofence is added and if the device is already inside that geofence.

Check the difference is the time:

DWELL = is already inside that geofence for some time.

ENTER = is already inside that geofence.

EXIT = is already outside that geofence.

Answer:

You can use

public GeofencingRequest getGeofencingRequest(Geofence geofence){
return new GeofencingRequest.Builder()
.addGeofence(geofence)
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL|
GeofencingRequest.INITIAL_TRIGGER_ENTER|
GeofencingRequest.INITIAL_TRIGGER_EXIT)
.build();
}

This function will trigger the GeofenceRequest when either of ENTER, DWELL, EXIT will take place