androidgeolocationgoogle-awareness

Fences not registering - google awareness API


I am currently registering a location fence on my application, similar to how it is explained on the documentation. Using logs, I can see the registering callback and the broadcast receiver are being correctly called. However, if I re-run the application, these are not fired. After rebooting my phone it works again.

I am not unregistering the fence because I want the fence to be fired even in the background (the receiver is not tied to an Activity).

¿How can I get this working even if I re-run the application multiple times during application development? ¿How can I ensure the fence is correctly registered when a user reinstalls or updates the application?

I create the AwarenessFence using

AwarenessFence allLocations = AwarenessFence.or(locationFences);

where locationFences is a collection of LocationFence objects created like this

singleLocationFence = LocationFence.entering(latitude, longitude, FENCE_RADIUS); 

Solution

  • I've done the fence-handling in a service and since a service can run in background it works fine. If "onDestroy" is called, I used to unregister the fences, so the operation-system will not have to observe these fences anymore. The service also solved your "rerun"-problem because it can only be on service at a time.

    Your next point with

    AwarenessFence allLocations = AwarenessFence.or(locationFences);
    

    its working, I tried it with a TimeFence

    AwarenessFence allLocations = AwarenessFence.or(TimeFence.inInterval(new Date()), Long.MAX_VALUE));
    

    But better use

    AwarenessFence allLocations = locationFences;
    

    since AwarenessFence is the parent of all BeaconFences, Geofences, TimeFencee, (...) form the Awareness API.