Android Question Geofence Service #StartAtBoot: True

Christian García S.

Active Member
Licensed User
Hello,

I am using Geofence Service (https://www.b4x.com/android/forum/threads/geofence-monitoring-a-region-in-the-background.84767/), before I worked with
B4X:
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
now I use
B4X:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="26"/>

And service code is this:

B4X:
#Region  Service Attributes
    #StartAtBoot: True
  
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.      
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
    GetGPS
    Service.StopAutomaticForeground
End Sub

Sub Service_Destroy

End Sub

Sub GetGPS
    Dim Jobnotify5 as HttpJob
    Jobnotify5.Initialize("GPSOnStart", Me)
    Jobnotify5.download2("http://" & Globals.ServerIP & "/app/beacons.php", _
    Array As String ("username", Globals.usuarioapp,"action", "gps"))  
    Log("getGPSCoordenadas on Start")
End Sub

Sub AddGeofence(resMap As Map)
    Dim lat, lon, loc As String
    Dim rad As Double
    Dim geo As Geofence
  
    lat = resMap.Get("latitude")
    lon = resMap.Get("longitude")
    rad = resMap.Get("radius")
    loc = resMap.Get("location")
  
    geo.Initialize
    geo.Id = loc
    geo.Center.Initialize2(lat, lon) 'change location!
    geo.RadiusMeters = rad
    geo.ExpirationMs = (DateTime.TicksPerDay)*365
    CallSubDelayed3(GeofenceService, "AddGeofence", Me, geo)
    Wait For Geofence_Added (Success As Boolean)
    Log("Geofence added on Start: " & Success)
End Sub

Sub JobDone(Job As HttpJob)
    Dim Resultado As List
    Dim DeviceName As String
    Dim Titlev As String
    Dim Contentv As String
    Dim HTMLv As String
    Dim Segv As Int  
  
    If Job.Success Then
        Dim res As String
        res = Job.GetString

        Log("Back from Job: " & Job.JobName )
      
        Log("Resultado:" & res)
          
        If res.contains ("[]") Or res = Null Or res.Length < 2 Then
            Log("Sin resultados")
        Else
            Log("Response from server: " & res)
            Dim JParser As JSONParser
            Dim resMap As Map
            JParser.Initialize(res)
          
            Select Job.JobName
      
                Case "GPSOnStart"
                    Resultado = JParser.NextArray 'returns a list with maps
                    For i = 0 To Resultado.Size - 1
                        resMap= Resultado.Get(i)
                        AddGeofence (resMap)                  
                        Log(resMap.Get("latitude"))
                        Log(resMap.Get("longitude"))
                        Log(resMap.Get("radius"))
                        Log(resMap.Get("location"))
                    Next

            End Select
        End If
      
    Else
        'ToastMessageShow("Error job main: " & Job.ErrorMessage, True)
        Log("Error job act_principal: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub

I recently put Service.StopAutomaticForeground and the application icon no longer appears as notification when the phone is restarted, but the alerts for entering the fence do not trigger.

I need to do something else inside the service so that it loads the geofences when I start the phone ???

Is there any way to visualize which are the active fences for that app or for the whole device?

Thank you
 
Top