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: 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:
    Code:
    AddPermission(android.permission.ACCESS_FINE_LOCATION)
    CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
    - Add geofences regions with code such as:
    Code:
    '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.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:
    Code:
    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: " & IdTrue)
    End Sub

    Public Sub Geofence_Exit (Id As String)
       
    Log("Exit: " & Id)
       
    ToastMessageShow("Exit: " & IdTrue)
       
    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:
    Code:
    #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.
     

    Attached Files:

    Last edited: Dec 30, 2018
  2. BillMeyer

    BillMeyer Active Member Licensed User

    @Erel,
    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

    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
     
  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

    Circle
     
    Beja likes this.
  10. Roberto P.

    Roberto P. Well-Known Member Licensed 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
     
  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.

    Code:
    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
    like
    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:

    upload_2017-10-10_14-38-21.png
     
Loading...
  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