In this example you will find what you need to achieve what you are searching for.
https://www.b4x.com/android/forum/threads/background-location-tracking.99873/
I tested it and even tried to modified two things : first send (lat, Long) to server but not worked, also trie to load image to notification bar but also not worked, any help, please , the code as following <code>
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Private nid As Int = 1
Private GPS As GPS
Private Tracking As Boolean = False
Private lock As PhoneWakeState
Private ServerURL As String = "
http://192.168.1.116/TrackSon/api.php"
Private UpdateTimer As Timer
Private LastLocation As Location
Private HasLocation As Boolean = False
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
GPS.Initialize("gps")
lock.PartialLock
' ── Timer:
UpdateTimer.Initialize("UpdateTimer", 5000)
UpdateTimer.Enabled = False
End Sub
Sub Service_Start(StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("جاري تحديد الموقع..."))
Track
End Sub
Public Sub Track
If Tracking Then Return
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If
GPS.Start(0, 0)
Tracking = True
UpdateTimer.Enabled = True
Log("GPS started, Timer started")
End Sub
Sub UpdateTimer_Tick
If HasLocation = False Then
Log("Timer tick — no location yet")
UpdateNotification("في انتظار GPS...")
Return
End If
Log("Timer tick — sending location")
DoUpdate(LastLocation)
End Sub
Sub GPS_LocationChanged(Location1 As Location)
Log("GPS LocationChanged")
LastLocation = Location1
HasLocation = True
End Sub
Private Sub DoUpdate(Location1 As Location)
Try
DateTime.DateFormat = "yyyy-MM-dd"
DateTime.TimeFormat = "HH:mm:ss"
Dim currentTime As String = DateTime.Time(DateTime.Now)
Dim body As String = $"Lat: $2.5{Location1.Latitude} / $2.5{Location1.Longitude}${CRLF}Last update : ${currentTime}"$
UpdateNotification(body)
Log(body)
SendLocationToServer(Location1)
Catch
Log("DoUpdate error: " & LastException)
End Try
End Sub
Private Sub UpdateNotification(Body As String)
Dim n As Notification = CreateNotification(Body)
n.Notify(nid)
End Sub
Sub SendLocationToServer(Location1 As Location)
Dim link As String = ServerURL & _
"?device_id=test123" & _
"&lat=" & NumberFormat2(Location1.Latitude, 1, 6, 6, False) & _
"&lng=" & NumberFormat2(Location1.Longitude, 1, 6, 6, False)
Log(link)
Dim job As HttpJob
job.Initialize("send", Me)
job.Download(link)
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Log("SUCCESS: " & Job.GetString)
Else
Log("FAILED: " & Job.ErrorMessage)
End If
Job.Release
End Sub
'═══════════════════════════════════════════════════════════════════════
' Notification — الحل الصحيح للصورة عبر RemoteViews
'═══════════════════════════════════════════════════════════════════════
Sub CreateNotification(Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tracking location :", Body, Main)
' ── Large Icon عبر NotificationCompat builder ──
Try
Dim bmp As Bitmap = LoadBitmap(File.DirAssets, "imgPlay.png")
Dim jo As JavaObject = notification
Dim builder As JavaObject
Dim found As Boolean = False
For Each fname As String In Array As String("builder", "mBuilder")
Try
builder = jo.GetField(fname)
If builder.IsInitialized Then
Dim androidBmp As JavaObject = bmp
builder.RunMethod("setLargeIcon", Array(androidBmp))
Log("LargeIcon set via: " & fname)
found = True
Exit
End If
Catch
Log("Field '" & fname & "' not found")
End Try
Next
If found = False Then
Try
jo.RunMethod("setLargeIcon", Array(bmp))
Log("LargeIcon set directly")
Catch
Log("Direct setLargeIcon failed: " & LastException.Message)
End Try
End If
Catch
Log("CreateNotification image ERR: " & LastException.Message)
End Try
Return notification
End Sub
Sub Service_Destroy
UpdateTimer.Enabled = False
If Tracking Then GPS.Stop
Tracking = False
lock.ReleasePartialLock
End Sub </code>