Android Tutorial Geofence - Monitoring a region in the background

Discussion in 'Tutorials & Examples' started by Erel, Oct 8, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:

    Configuration steps
    - Add GeofenceService from the example project to your app.
    - Add this code to the manifest editor:
    CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
    - Add geofences regions with code such as:
    'Geofence regions are defined as a circle with a center point and radius.
    Sub AddGeofence
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
    Dim geo As Geofence
           geo.Id = 
    32.837235.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:
    Public Sub Geofence_Enter (Id As String)
    Dim n As Notification
       n.Icon = 
    "Enter: " & Id"Enter", Main)
    ToastMessageShow("Enter: " & IdTrue)
    End Sub

    Public Sub Geofence_Exit (Id As String)
    Log("Exit: " & Id)
    ToastMessageShow("Exit: " & IdTrue)
    Dim n As Notification
       n.Icon = 
    "Exit: " & Id"Exit", Main)
    End Sub
    - Add to the main module:
    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.

    Attached Files:

    Last edited: Dec 30, 2018
    f0raster0, MarkusR, jimmyF and 17 others like this.
  2. BillMeyer

    BillMeyer Well-Known Member Licensed User

    I believe there is such a thing as Telepathy - Thank you so much - 100% on time !! - Now I can complete my app.
  3. Erel

    Erel Administrator Staff Member Licensed User

    This explains the strange vibes I received today...
  4. ivanomonti

    ivanomonti Well-Known Member Licensed User


    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:
  5. Erel

    Erel Administrator Staff Member Licensed User

  6. HAH

    HAH Active Member Licensed User

    WOW! Great Marketing tool :)
  7. Beja

    Beja Expert Licensed 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?
  8. Cassie

    Cassie Member Licensed User

    Hello guys, can this support multiple location. Thanks
  9. DonManfred

    DonManfred Expert Licensed User

    Beja likes this.
  10. Roberto P.

    Roberto P. Well-Known Member Licensed User

    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:
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(
    at anywheresoftware.b4j.object.JavaObject.RunMethod(
    at b4a.geofence.geofenceservice._service_create(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent(
    at b4a.geofence.geofenceservice.onCreate(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(

  11. Erel

    Erel Administrator Staff Member Licensed User

    Yes. Just make sure to set a different id for each geofence.

    See post #5.
  12. Beja

    Beja Expert Licensed User

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

    geo.Center.Initialize2(32.837235.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: Oct 10, 2017
  13. DonManfred

    DonManfred Expert Licensed User

    Beja likes this.
  14. imbault

    imbault Well-Known Member Licensed User

    Why the doc says for :

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

    My library is too old? (GPS lib 1.20)
  15. DonManfred

    DonManfred Expert Licensed User

  16. Beja

    Beja Expert Licensed User

    Still the question was about this library and the accompanied example. General discussion and solutions of rect and even more than 4 side polygons with examples are plenty in the forum.. Ilan and others contributed with many examples. Thanks for trying to help.
  17. DonManfred

    DonManfred Expert Licensed User

    Yes, i know.

    But the documentation does not provide Information on how to (or even if possible) setup a rectangular area.
  18. Beja

    Beja Expert Licensed User

    Just wondering who's talking about documentation..
  19. DonManfred

    DonManfred Expert Licensed 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)?

    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 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.
  20. imbault

    imbault Well-Known Member Licensed User

    No, I didn't ask that, I just said there is a pb with the doc, concerning the lat & long parameters:

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice