public final class GeofenceManager
- Object
- GeofenceManager
ImplementsIterable<Geofence>
A utility class to simplify Geofencing in Codename One. Using this class to manage an app’s Geofences works around the 20-region limit on iOS and 100-region limit on Android, so that your app can monitor an unlimited number of Geofences simulataneously.
How it Works
GeofenceManager maintains a “bubble” region around the current device location. You may register as many regions as you like to be monitored with GeofenceManager, but it will only register the regions that intersect the current “bubble” region. When you exit the bubble region, the GeofenceManager will clear all of the previously registered regions, create a new bubble, and then register only those regions that intersect this new bubble.
GeofenceManager uses Storage to maintain its own active list of regions.
Limitations
GeofenceManager will only register 19 regions at a time, so if more than 19 regions intersect the current “bubble”
region, some of them won’t make the cut. You can set the radius of the “bubble” region using #setBubbleRadius(int)
to increase or decrease the “bubble” region area, so that no regions are left behind.
Although you can set any positive radius value you like, a typical Android or iOS device has a minimum effective radius of about 100m.
Note: If your app uses GeofenceManager, you shouldn’t also add your own Geofences manually using com.codename1.location.Geofence)
as your manual regions may conflict.
Usage
GeofenceManager mgr = GeofenceManager.getInstance(); mgr.setListenerClass(MyGeofenceListener.class); mgr.add(geofence1, geofence2, geofence3); mgr.update(10000);
And the MyGeofenceListener class should be an instance of Geofence.
Reloading Geofences Upon Exiting Bubble
While there is no absolute limit on the number of regions that you can register in GeofenceManager
simulataneously, since it is actually storing the list of Geofences in Storage, there is a practical limit. E.g.
It probably wouldn’t perform well if you stored several thousand at a time. If you want to monitor large quantifies
of regions (thousands, or millions), you can simply respond to the GeofenceListener#onExit(java.lang.String) event
for the “bubble” region, and “reload” the GeofenceManager with new regions related to the device’s current location.
You might load the new locations from a web-service, for example. Use the #isBubble(java.lang.String) method
to check if the id parameter is for the bubble region, and act accordintly.
Nested types
class GeofenceManager.Listener | The Listener class that is registered to receive Geofence events. |
Methods
Inherited methods
Method details
getInstance
public static GeofenceManager getInstance()getBubbleRadius
public int getBubbleRadius()Returns
setBubbleRadius
public void setBubbleRadius(int bubbleRadius)Parameters
bubbleRadiusint- the bubbleRadius to set
getBubbleExpiration
public long getBubbleExpiration()Returns
setBubbleExpiration
public void setBubbleExpiration(long bubbleExpiration)Parameters
bubbleExpirationlong- the bubbleExpiration to set
isBubble
public boolean isBubble(String id)Parameters
idString- An ID to check.
Returns
getListenerClass
public synchronized Class<? extends GeofenceListener> getListenerClass()setListenerClass
public synchronized void setListenerClass(Class<? extends GeofenceListener> c)add
public synchronized void add(Geofence... geofence)add
public synchronized void add(Collection<Geofence> geofences)isCurrentlyActive
public synchronized boolean isCurrentlyActive(String id)remove
public synchronized void remove(String... ids)remove
public synchronized void remove(Collection<String> ids)clear
public synchronized void clear()size
public synchronized int size()asMap
public synchronized Map<String, Geofence> asMap()asList
public synchronized List<Geofence> asList()asSortedList
public synchronized List<Geofence> asSortedList()refresh
public synchronized void refresh()update
public synchronized void update(int timeout)Parameters
timeoutint- Timeout (in milliseconds)
update
public synchronized void update(int timeout, boolean forceRefresh)Parameters
timeoutint- Timeout (in milliseconds)
forceRefreshboolean- If true, then this will force removal and re-addition of all geofences.
iterator
public Iterator<Geofence> iterator()Returns
Iterator instance.