Android Tutorial Geofence - Monitoring a region in the background

Status
Not open for further replies.
Geofencing is an Android feature that allows your app to be notified when the user enters or exits a monitored region.

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.

The GeofenceService uses JavaObject to implement the Geofencing API: https://developer.android.com/training/location/geofencing.html

Configuration steps
- Add GeofenceService from the example project to your app.
- Add this code to the manifest editor:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
- Add geofences regions with code such as:
B4X:
'Geofence regions are defined as a circle with a center point and radius.
Sub AddGeofence
   rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       Dim geo As Geofence
       geo.Initialize
       geo.Id = "Test1"
       geo.Center.Initialize2(32.8372, 35.2698) 'change location!
       geo.RadiusMeters = 100
       geo.ExpirationMs = DateTime.TicksPerDay 'expire after one day
       CallSubDelayed3(GeofenceService, "AddGeofence", Me, geo)
       Wait For Geofence_Added (Success As Boolean)
       Log("Geofence added: " & Success)
   End If
End Sub
- Add the event subs to the starter service:
B4X:
Public Sub Geofence_Enter (Id As String)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo("Enter: " & Id, "Enter", Main)
   n.Notify(1)
   ToastMessageShow("Enter: " & Id, True)
End Sub

Public Sub Geofence_Exit (Id As String)
   Log("Exit: " & Id)
   ToastMessageShow("Exit: " & Id, True)
   Dim n As Notification
   n.Initialize
   n.Icon = "icon"
   n.SetInfo("Exit: " & Id, "Exit", Main)
   n.Notify(2)
End Sub
- Add to the main module:
B4X:
#AdditionalJar: com.google.android.gms:play-services-location

Note that monitored regions are cleared when the device is restarted. You can add another service with #StartAtBoot set to true and add the regions there.

Update: Project updated with new permission required in Android 10+ and another change related to the PendingIntent created internally.
 

Attachments

  • Geofence.zip
    8.8 KB · Views: 191
Last edited:

Beja

Expert
Licensed User
I found a workaround, that one can define a series of overlapped circular fences with radius of .5 * the desired rectangular width.. The number if fences corresponds to the desired resolution of the rectangle.
 

Beja

Expert
Licensed User
Hi Erel,
Today I tried to run the example and on one phone it just closed without notification, and on another one it said "Unfortunately, B4A Example has stopped. Open app again"
any help appreciated.
Using B4A 7.01
 

DonManfred

Expert
Licensed User
check the unfiltered logs also
 

Christian García S.

Active Member
Licensed User
Hello, It have been implented this library in a marketing project and works perfect, but I need this library for a logistic project.

I have a question, the event Geofence is launched one time when the user enter to the geofence.

There are a way for asking which is geofence I am inside, is for validation porpuses, I know I can make a variable and save this information.

For example if the app is closed or the phone is restarted and I receive the notification, I can not use this variable for some validation, because the event is only triggered one time.

Or can you give me an idea how I can validate location??

Thanks for your help.

Regards,
Christian
 
Last edited:

DonManfred

Expert
Licensed User
There are a way for asking which is geofence I am inside
The event which raise contains the ID of the Fence you entered....
It is the ID you set when creating the Fence.

B4X:
   Dim geo As Geofence
   geo.Initialize
   geo.Id = "Test1" ' ID is here....
   geo.Center.Initialize2(32.8372, 35.2698) 'change location!
   geo.RadiusMeters = 100
   geo.ExpirationMs = DateTime.TicksPerDay 'expire after one day

B4X:
Public Sub Geofence_Enter (Id As String)
   Log("Enter: " & Id)
End Sub

Public Sub Geofence_Exit (Id As String)
   Log("Exit: " & Id)
End Sub
 

BillMeyer

Well-Known Member
Licensed User
Good Day,

I have a situation where I place a GeoFence at an Accident Scene. The time that it takes for the Accident to be cleared up is variable. After the clean up I no longer need the GeoFence and need to expire it.

Will it suffice to re-write/over write the fence and then just alter the ExpirationsMs to DateTime.Now ? or is there another way of doing this ?

If I need to create a new thread, I will, please advise.
 

Johan Hormaza

Well-Known Member
Licensed User
I have this problem on Android 8.1

--------- beginning of crash
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **

com.google.android.gms.common.api.ApiException: 1000:
Geofence added: false


And in a Xiaomi Redmi Note 8 Android 9 it does not tell me if I am inside or outside the Geofence
 
Status
Not open for further replies.
Top