Android Question Erel's Geofence and updating a label text value with some data.

Guest

New Member
Hi all... Long time lurker here.

I am trying to get Erel his Geofence demo to update a simple label text value (Geofence tutorial used is this one: https://www.b4x.com/android/forum/threads/geofence-monitoring-a-region-in-the-background.84767/ )

The demo has 3 modules. Main, GeofenceService and Starter. I have only made changes to Main and Starter.

I have added to the Starter module
B4X:
Sub Process_Globals
    Dim result As String
    result = "0"
End Sub

The Geofence_Enter sub looks like so (added result = Id and StartActivity(Main) )
B4X:
Public Sub Geofence_Enter (Id As String)
    Log("Enter: " & Id)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Enter: " & Id, "Enter", Main)
    n.Notify(1)
    ToastMessageShow("Enter: " & Id, True)
    result = Id
    StartActivity(Main) 'show the main activity again
End Sub

The Geofence_Exit sub looks like so (Again added result = Id and StartActivity(Main) )
B4X:
Public Sub Geofence_Exit (Id As String)
    Log("Exit: " & Id)
    ToastMessageShow("Exit: " & Id, True)
    result = Id
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.SetInfo("Exit: " & Id, "Exit", Main)
    n.Notify(2)
    StartActivity(Main) 'show the main activity again
End Sub

In the Main module I added to Erls code.
B4X:
Sub Globals
    Dim lblGeo As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
    lblGeo.Visible = True
    AddGeofence
End Sub

And added some test Geofence locations with geo.id 1, 2 and 3 (replaced the true GPS locations in the example below)
B4X:
Sub AddGeofence
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
'TEST LOCATION 1
        Dim geo As Geofence
        geo.Initialize
        geo.Id = "1"
        geo.Center.Initialize2(xx.392218, y.208352) 'THUIS!
        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)
'TEST LOCATION 2
        Dim geo As Geofence
        geo.Initialize
        geo.Id = "2"
        geo.Center.Initialize2(xx.390509, y.208845)
        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)
'TEST LOCATION 3   
        Dim geo As Geofence
        geo.Initialize
        geo.Id = "3"
        geo.Center.Initialize2(xx.391098, y.212085)
        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


Obviously I also created a main layout with a label named lblGeo in/on it.

The Sub Activity_Resume looks like this.
B4X:
Sub Activity_Resume
    If Starter.result = "0" Then
        lblGeo.Text = "No data"
    End If
    If Starter.result = "1" Then
        lblGeo.Text = "location 1"
    End If
    If Starter.result = "2" Then
        lblGeo.Text = "location 2"
    End If
    If Starter.result = "3" Then
        lblGeo.Text = "location 3"
    End If
End Sub

The ToastMessageShow in Geofence_Enter and Geofence_Exit are working (most of the times), and the lblGeo.Text does get update (again most of the times) when starting the app in the corresponding location, however it does not change when moving to another location. That is until is restart the app..

I'm sure that I am making a beginners ID 10 T mistake, and hope some one here can show me what.

Thank you, and please stay safe!
 
Last edited:

Guest

New Member
Hi Erel,

As in the ToastMessage from the Geofence_Enter and Geofence_Exit sub in the Starter module. Than this is a yes.
It does show the change regions.

I have attached the file I'm working on. Please note that I have removed the GPS coordinates.
Again. It is probably a stupid error I've made..
Once again. Thank you.
 

Attachments

  • Geofence.zip
    16.6 KB · Views: 312
Upvote 0

Guest

New Member
Question. Is the Sub Activity_Resume the correct place for the if than statements to change lblGeo.Text when needed?

The requested logs. (as you can see Activity_Resume does log changes)
B4X:
Logger connected to:  HTC HTC U12+
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Activity_Resume: 0
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
Activity_Resume: 0
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **
Geofence added: true
Geofence added: true
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Activity_Resume: 0
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **
Geofence added: true
** Service (geofenceservice) Start **
Enter: 1
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Activity_Resume: 1
Geofence added: true
** Activity (main) Pause, UserClosed = false **
** Service (starter) Destroy (ignored)**
** Service (geofenceservice) Destroy **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Activity_Resume: 0
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **
Geofence added: true
** Service (geofenceservice) Start **
Enter: 1
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Activity_Resume: 1
Geofence added: true
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Activity_Resume: 1
** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Activity_Resume: 0
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As you can see in these logs:
Enter: 1
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Activity_Resume: 1

Activity_Resume is called after you call StartActivity. Maybe there is an extra space or something in the id.

Add another label and set it with lblNew.Text = Starter.result.

Note that in Android 10+, StartActivity will not do anything if the app is in the background.
 
Upvote 0

Guest

New Member
Once again thank you Erel.
lblNew does show the corresponding geo.Id, and no "whitespace" found in the Id.
The phone I'm testing on does run Android 10.

Time to find an older phone and test again.
I will report back when I have more info.

For now... תודה
 
Upvote 0
Top