android / location-samples

Multiple samples showing the best practices in location APIs on Android.
Apache License 2.0
2.7k stars 2.78k forks source link

Multiple initial triggers on geofences doesn't seem to work. #103

Open catt-stefano opened 6 years ago

catt-stefano commented 6 years ago

I'm trying to use the geofencing apis and the initial trigger functionality is not behaving correctly when setting multiple initial triggers. In my application I want to be notified of a geofence transition (enter or exit) whenever I add a geofence, depending on my actual location being inside or outside the geofence at the moment when the geofence gets added.

Looking at the documentation I see that it is possible to use the method setInitialTrigger with a bit wise argument. I observed that the combination of INITIAL_TRIGGER_ENTER and INITIAL_TRIGGER_EXIT with the bitwise OR operator does not produce the correct behaviour: I only get enter events but never any exit event. The only way to have exit events on geofence addition, is to only use the INITIAL_TRIGGER_EXIT argument alone. Some more info: the way I'm using the geofence API is removing (by pending intent) and adding the geofences I want to get updates on. This sometimes means removing all the geofences and immediately (in the onSuccessListener callback) adding a new set.

catt-stefano commented 6 years ago

I have more info on this: what is stopping the initial exit trigger to happen, seems to be the timing of the geofence remove and add procedures. I observed that, even if I wait for the onSuccess callbacks of the remove geofence method, and add the geofences afterwards, the whole process produces the correct behaviour only if it wasn't already done recently. Unfortunately, the various callback listeners of the remove or add methods are of no help in figuring out how long you have to wait before adding the geofences. Is there a way of having a guaranteed initial trigger exit?

A thing I have already tried:

I'm using 11.4.2 of play services.

shailen commented 6 years ago

Thanks for letting us know. I'll take a look.