I have a problem understanding Androids geofences.
Actual Problem: I used Enter
+Exit
events from googles geofence-api, but on many devices the signal is so inaccurate that it jumps in and out a fence (jumps are greater than radius 400m often).
Planned Solution: So I want to use Dwell
to "smooth" this. If the location keeps inside a fence for a minute, dwell happens. So far so good. But how do I detect the leave of a fence? When I use Exit
, several Exit
can happen due to those signaljumps. What I need is a kind of "undwell", when I leave the geofence for more than a minute.
I want to avoid reimplementing the whole Geofence with custom logic that registers on fast repeated geo-locations and filter out small outliers.
Question: Is there something in the geofence-api to achieve an "undwell"? Or is there a best practice how to check if an Exit
is a real exit?
public long lastEnterTime = 0;
public void initTimer(){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(lastEnterTime!=0 && System.currentTimeMillis() > lastEnterTime + 1000*60){
lastEnterTime = System.currentTimeMillis();
onRealEnter();
}
}
},0,1000);
}
public void onRealEnter(){
}
public void onEnter(){
lastEnterTime = System.currentTimeMillis();
}
public void onExit(){
lastEnterTime = 0;
}
In Google Geofences, Enter event triggers every time you enter a geofence, Exit event triggers every time you cross the boundry of the Geofence. Also Dwell event triggers when you stay inside a geofence for a specified interval of time.
In your case, if you keep jumping In and Out of a geofence in less than one minute, Dwell will never happen.
If you enter a geofence, Enter event triggers, and then you stay inside for one minute or more, Dwell will happen, and after the Dwell happens, if you notice any Exit event, you can use that Exit event as a real Exit.
Note: You can also increase the radius of Geofence.
I hope my answer will be clear to you. :)
This is just an idea, but for every area you could register two geofences with different radius. Register to listen for DWELL for the geofence with the smaller radius, and for EXIT for the other one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With