Android Question Geofence monitor from background

GraemeW

Member
Licensed User
I can't get Geofence monitoring to work when the app is closed, using the example app https://www.b4x.com/android/forum/threads/geofence-monitoring-a-region-in-the-background.84767/ with only the location changed.

Entry/exit monitoring works fine on a Samsung S9 when the app is open but it seems this statement is no longer true:
"The nice thing about this feature is that your app doesn't need to run for monitoring to work.
It will be started automatically when needed."

Does anyone have a solution?
 
Last edited:

Sandman

Expert
Licensed User
Longtime User
don't kill the app yourself
I've seen you mention this in another thread also. But I haven't seen anything official from Google about it.

Is it a fact that self-killing an app also kills the registered geofences, or what's the situation here? Do we know if this is documented somewhere official?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
or what's the situation here?
Android will mark the app as "crashed" (kind of).
A crashed app does not get started by a Broadcast or Intent.
 
Upvote 0

GraemeW

Member
Licensed User
Thanks guys for your good advice and info. I'm developing a tourist app for an island which in part aims to measure ferry queue waiting time and parking congestion at three locations. The geofence feature would seem to be the best solution if I can get it to work reliably..

I did some tests this morning using my house as the location in the example geofence app and driving a few hundred metres away and returning (in Release (obfuscated) mode). These are the results:

1. app left open all night and other apps opened in the morning - fail
2. app opened and visible - success
3. app open but then sent behind another visible app - fail
4. repeated 2 but behind lock screen - success

I habitually "close all" recent apps and if this is the same as self-kill then it seems I will have a problem, together with a user cohort that may have the same habit. In any case, at least on my phone, the example app only works if it is visible.

I have got background geofencing to work by converting the GeofenceService to a foreground service that is scheduled to run daily from 6 am to 9 pm (covering the ferry operating hours) - after working on these issues months ago (it's taken me that long to raise the problem here!). However this is a relatively clumsy approach which brings out all the people worried that I'm tracking their activities - nevermind that I say explicitly in the app that I'm not, and only counting the presence of their phone in three specific locations with no personal info - they still don't believe me...

The Google info at https://developer.android.com/training/location/geofencing.html talks about using a broadcast receiver with a manifest entry - which I may research further but Erel's and DonManfred's comments suggest that may not work for all users either...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The Geofence code doesn't use a broadcast receiver. It seems like it is a new change or requirement. However depending on your use case a foreground service might be the best solution as without it the responsiveness of geofencing while the app is in the background is:
"The average responsiveness for a geofencing event is every couple of minutes or so."
https://developer.android.com/about/versions/oreo/background-location-limits.html
 
Upvote 0
Top