Android Geofencing: Trigger only while outside geofence - android

I am making a geofencing application, which must trigger notification constantly while the device is outside the geofence, not only on exit.
I have tried using Geofencing API through the training example given in Google android developer training page. But, I got notification only when the device enters, dwell or exit.
According to what I have researched till now in both SO and google, there are only three types of transitions handled in Geofencing API: Enter, Dwell and Exit. And the notification starts only when device enters the geofence, till it exits.
Can anyone help me out how to make it work?

That is not possible that way. It does not make any sense outside the geofence area. That is not an usecase of geofence.
You can try to specify broader range of the area and then check the geocoordinates if this is outside of your area you want to have.

Incase you want your own custom exit trigger, when you are outside the fence. You can write your custom action by measuring distance(lat,long) between your current location(FusedLocationProvider) and the fence location (lat, long).
You can use below methods available in Android Location API to measure the difference and generate a custom trigger to indicate that you are outside the fence.
Typically this is what you need to do
if[ (difference in location) > (Radius of your geo fence) ] -> Custom Exit of Fence Action
distanceTo
distanceBetween (Provide both your current lat,long and your fence lat,long here)

Related

geofencing dwell event never received when adding geofence with device within the radius

I made a simple test app, setup location and geofencing monitoring using LocationServices.getFusedLocationProviderClient and LocationServices.getGeofencingClient.
I added a geofence using just the dwell transition notification and initial trigger, with the right position and radius such that I was expecting the 'dwell' event very soon after adding the geofence:
geofenceBuilder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL);
...
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
In device log I noticed several of these two, each time position was obtained:
The geofencing pending intent was never triggered. The device was on my desk.
I didn't understand what's going on. I even tried to switched to Google Maps and switched back to the app, it didn't do anything. The device is clearly not in any deep sleep since I am using it. So what's in deep sleep? I am guessing some geofencing component in Google Play Services? But the location is being queried constantly by the app, so why doesn't the Google Play Services geofencing sees I am inside the location and send the 'dwell' event?
After I moved around the house with the device, the two log entries didn't appear anymore and I got the geofencing dwell event!
My conclusion is if device\Android component is in this "sleep" mode when a geofence is being set, I don't think the geofencing events won't fire, at least not the 'dwell' per my tests, as I am writing this I haven't tested to see if 'enter' is called.
What is this "deep sleep mode" and "GeofencerStateMachine" ? Is it from Android Google Play services? How does it work? What's the reason I didn't get the 'dwell' event?
On another device (Samsung Galaxy) I see same behavior, the dwell event is not received, but there's no similar log entry with 'deep sleep' and moving the device around doesn't make it receive the 'dwell' event...…
I am logging the device coordinates and I can clearly see the accuracy being around 10 and yet, the dwell event is not being sent.
Update: I left the device on desk for like 40 mins. Got back, opened the app, started the geofence, and again, no dwell event being received and now there's not even the log event saying "Location dropped because in deep still mode"... I'm lost...
Update 2: Even after reinstalling the app it doesn't make the dwell nor the enter events to be received (I also added monitoring for 'enter')
Note : Dwell event only occur if you are not already inside the geofence while registering, It will work if you walk inside the geofence after registratoin.
You can also try using fake gps to test this and keep on changing the location, worked for me all the time.
I have tried following solutions and both worked for me.
1st solution is same as your case, as you want to have only dwell event.
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
builder.setLoiteringDelay(500);
you also have to add this method(time in millis)
2nd solution is for who would like to have all three events 'ENTER','DWELL' & 'EXIT'
follow the code snippet
// Register your initial trigger with both 'ENTER' & 'DWELL' events
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);
//and then set transition types for all three events
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_DWELL |
Geofence.GEOFENCE_TRANSITION_EXIT);
//dont forget to add the setLoiteringDelay, in millis
.setLoiteringDelay(5000);
Hope this will for you guys as well.
I can provide complete working solution if anyone needs it please do comment.

How does Google Fences API work?

I want to ask a simple question.
How does Google Fences API (https://developers.google.com/awareness/android-api/fence-api-overview) work? Espescially the activity recognition.
Thanks
As the Fence API Overview mentions:
...the concept of "fences" is taken from geofencing, in which a
geographic region, or "geofence", is defined, and an app receives
callbacks when a user enters or leaves this region.
The Fence API
expands on the concept of geofencing to include many other context
conditions in addition to geographical proximity.
An app receives
callbacks whenever the context state transitions. For example, if your
app defines a fence for headphones, it will get callbacks when the
headphones are plugged in, and when they are unplugged.
In basic terms you define a geographical area (defined by GPS coordinates) so that when a user enters or leaves this "geospace" a notification is sent out. See the following image

Android Tracking App - check if someone is getting closer or distancing from specific point

I created an android app, which stores user's home location and count distance between home and his/her actual location, in a Service class.
Now I would like to programme feature that would decide based on counted distance, if a user is:
at home
away
getting closer
distancing from home
This should be similar to Google Now, that can predict that you are for example going home. If someone had some examples, I would be sooo grateful
To implement such kinda work, you need to implement GeoFence in your android app.
GeoFence gives you idea about user entered/exited from a defined area. If you set home location as base location and radius to 500m then it gives you entered callback you are within 500m of your home.
When you go away 500m from your home location then it gives you exited callback. To know about whether you are home location then at a interval of few minutes you can poll GPS coordinates then can identify whether you are at your home location.
https://developer.android.com/reference/com/google/android/gms/location/Geofence.html

Proximity Alert

I want to make an application for Android 2.2+, utilizing the GPS. Basically I want to be able to set a specific zone in the app, using google maps.
Example: I am walking to the store, as soon as I am x (variable) meters away from my house I want an alarm going off on my phone.
I googled and saw that you can use proximity alerts to get a notification when you are close to a friends house or something like that. They used specific coordinates for this. I want to be able to draw a line on a map or use a street name, and as soon as i cross that line i want an alarm to go off. If anyone could point me to some tutorial for that part or give some advice, that would be great.
I think you need to map your line in google maps yourself with some formula.
Proximity alerts work with a radius. You might need to set up multiple points with a small radius along your line.
You will get either the entering Intent or the exiting intent.
http://developer.android.com/reference/android/location/LocationManager.html#KEY_PROXIMITY_ENTERING

Appcelerator Titanium Android background location service

I am trying to make an app , where if the user enters a particular region, he will get an alert. And that will work even if the app is in background.
I found Ti.App.iOS.backgroundService to do that for iphone but how to start the background service in android and how to present local notification kind thing when particular latitude and longitude is found.
so basically my questions are :-
background location service in android
android equivalent for localnotification
For background geo on Android I use the below strategy.
1) In my app.js I add an Titanium.App.addEventListener that contains my geo logic
Kitchen Sink Sample https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/app_events.js
2) I then create a ServiceIntent that fires every 15 minutes
Kitchen Sink Sample https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/android_services.js
Please note this service.js you call needs to be in your android folder, even if you have an android only project.
3) The service checks if the individual has moved more then 1KM, if they have it fires the App Event defined in step 1.
Using this strategy and adjusting my service time and distance calculation I've been able to reduce most of my battery drain issues that I had with the built in location event with distance filter.
Hope this helps.

Categories

Resources