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.

Updates:

- Uses a receiver instead of service. Services will fail on newer version of Android.
 

Attachments

  • Geofence.zip
    15.6 KB · Views: 502
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
error

B4A Versione: 7.30
Analisi del Codice. (0.01s)
Compilazione del codice. (0.17s)
Compilazione del codice di layouts (0.05s)
Organizzazione Librerie. Error
Maven artifact non trovato: com.android.support/support-v4
 

Beja

Expert
Licensed User
Longtime User
Hi Erel
Thanks so much for this library..
Can the geofence be a rectangle also, or it has to be a circle like in the example?
 

DonManfred

Expert
Licensed User
Longtime User

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi,
I have this error at runtime:

Logger connected to: db188dbe
--------- beginning of system--------- beginning of main~i:*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (geofenceservice) Create ***
geofenceservice_service_create (java line: 301)
java.lang.RuntimeException: Method: getGeofencingClient not found in: com.google.android.gms.location.LocationServices
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at b4a.geofence.geofenceservice._service_create(geofenceservice.java:301)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.geofence.geofenceservice.onCreate(geofenceservice.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3274)
at android.app.ActivityThread.access$1900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1559)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6134)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

thank
 

Beja

Expert
Licensed User
Longtime User

Hi Don
I thought the following can only draw either a circle or a square (equal sided rectangle) but not any rectangle.

B4X:
geo.Center.Initialize2(32.8372, 35.2698) 'change location!
geo.RadiusMeters = 100

My question was about if it's possible to pass 4 corners of a rectangle (4 lat/longs) as an array. So any shape of rectangle can be defined.
 
Last edited:

imbault

Well-Known Member
Licensed User
Longtime User
Why the doc says for :

geo.Center.Initialize2(Latitude, Longitude)
Latitude (String) should be in Degrees, Minutes, Seconds
like
L1.Initialize2("45:30:30", "45:20:15")

My library is too old? (GPS lib 1.20)
 

DonManfred

Expert
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User
@imbault was asking how to setup a rectangular area. I answered to look the documentation.
Here you see that there is nothing stated which enables a rectangular area. Or maybe i did not found any hint on this in the docu (oversee)?

Still the question was about this library and the accompanied example
As Erel wrote the lib i was sure he did a full wrap for this feature. Everything about this feature you can read in the Google Documentation.

Just wondering who's talking about documentation..
Just wondering why you asking this. As the lib is based on an Android Feature (which documentation can be found on the link i posted) there is nothing Erel can do (i guess).

Check the docu! This feature does is not implement a Rectangular area.
No need to write more about it. Ask google to add this feature if you need it.

This is my last answer to this thread.
 

imbault

Well-Known Member
Licensed User
Longtime User
No, I didn't ask that, I just said there is a pb with the doc, concerning the lat & long parameters:

upload_2017-10-10_14-38-21.png
 
Status
Not open for further replies.
Top