public class GeofenceManager extends Object implements Iterable<Geofence>
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.
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 LocationManager.addGeoFencing(java.lang.Class, com.codename1.location.Geofence)
as your manual regions may conflict.
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.
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.
Modifier and Type | Class and Description |
---|---|
static class |
GeofenceManager.Listener
Deprecated.
For internal use only.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Collection<Geofence> geofences)
Adds a set of regions to be monitored by GeofenceManager.
|
void |
add(Geofence... geofence)
Adds a set of regions to be monitored by GeofenceManager.
|
List<Geofence> |
asList()
Returns the Geofences as a list.
|
Map<String,Geofence> |
asMap()
Returns the Geofences as a Map.
|
List<Geofence> |
asSortedList()
Returns all Geofences sorted by distance from the current location.
|
void |
clear()
Removes all current regions.
|
long |
getBubbleExpiration()
Gets the expiration duration (in milliseconds) of the bubble region.
|
int |
getBubbleRadius()
Gets the radius of the "bubble" region, in metres.
|
static GeofenceManager |
getInstance()
Obtains reference to the singleton GeofenceManager
|
Class<? extends GeofenceListener> |
getListenerClass()
Gets the currently registered Listener class.
|
boolean |
isBubble(String id)
Checks if the given ID is for the "bubble" region.
|
boolean |
isCurrentlyActive(String id) |
Iterator<Geofence> |
iterator()
Iterates over all geofences that are being monitored.
|
void |
refresh()
Reloads geofences from storage.
|
void |
remove(Collection<String> ids) |
void |
remove(String... ids)
Removes a set of regions (by ID) so that they will no longer be monitored.
|
void |
setBubbleExpiration(long bubbleExpiration)
Sets the expiration duration (in milliseconds) of the bubble region.
|
void |
setBubbleRadius(int bubbleRadius)
Sets the radius of the "bubble" regin, in metres.
|
void |
setListenerClass(Class<? extends GeofenceListener> c)
Sets the GeofenceListener class that should receive Geofence events.
|
int |
size()
Checks the number of regions that are currently being monitored.
|
void |
update(int timeout)
Updates the active Geofences that are being monitored on the OS.
|
void |
update(int timeout,
boolean forceRefresh)
Updates the active Geofences that are being monitored on the OS.
|
public static GeofenceManager getInstance()
public int getBubbleRadius()
public void setBubbleRadius(int bubbleRadius)
bubbleRadius
- the bubbleRadius to setpublic long getBubbleExpiration()
public void setBubbleExpiration(long bubbleExpiration)
bubbleExpiration
- the bubbleExpiration to setpublic boolean isBubble(String id)
id
- An ID to check.public Class<? extends GeofenceListener> getListenerClass()
public void setListenerClass(Class<? extends GeofenceListener> c)
c
- public void add(Geofence... geofence)
geofence
- public void add(Collection<Geofence> geofences)
geofences
- public boolean isCurrentlyActive(String id)
public void remove(String... ids)
ids
- public void remove(Collection<String> ids)
public void clear()
public int size()
public List<Geofence> asSortedList()
public void refresh()
public void update(int timeout)
timeout
- Timeout (in milliseconds)public void update(int timeout, boolean forceRefresh)
timeout
- Timeout (in milliseconds)forceRefresh
- If true, then this will force removal and re-addition of all geofences.