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);
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.
I understand that after you register fences with Awareness API, you'd like to receive callbacks even you re-run the app, or if the app is in the background. The question does not clarify this but I believe if you're following the docs, you're registering the broadcastreceiver dynamically .
You may achieve what you intend by doing static registration of your broadcast receiver by adding something like this in the manifest file :
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
This way, you do not need to unregister the receiver in onDestroy(), and your app will get fence callbacks if you have registered fences, even if app is stopped or updated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.