The background location tracking example code works fine in its own. The service stays alive and it keeps on getting GPS location as well. What I want to do is send this location to a web service to save the location data. The code to fetch location is unchanged from the example. The only addition is SendLocationData function.
The above code is working fine on the tested Samsung phone, even when the screen is off. But on Realme and Redmi phones, this works only the first time i.e. when the app is installed and launched. After that, while it does get location from GPS (as notification is still there), the server is not getting data, even when screen is on.
This makes me wonder if Wait For (job) JobDone(job As HttpJob) does not work while the app is not in the foreground. Any other reason why this request is not going through?
B4X:
Sub GPS_LocationChanged (Location1 As Location)
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerMinute Then
Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
n.Notify(nid)
LastUpdateTime = DateTime.Now
SendLocationData(Location1.Longitude,Location1.Latitude)
End If
End Sub
Sub SendLocationData(Lon As Double,Lat As Double)
Dim userid As String
ToastMessageShow("Sending location data to server...",True)
userid="21"
Dim job As HttpJob
job.Initialize("",Me)
job.PostString("http://apiservice.com/location_mapper.php","user_id=" & userid & "&longitude=" & Lon & "&latitude=" & Lat)
Wait For (job) JobDone(job As HttpJob)
If job.Success Then
Log(job.GetString)
Else
ToastMessageShow(job.ErrorMessage,True)
End If
job.Release
End Sub
The above code is working fine on the tested Samsung phone, even when the screen is off. But on Realme and Redmi phones, this works only the first time i.e. when the app is installed and launched. After that, while it does get location from GPS (as notification is still there), the server is not getting data, even when screen is on.
This makes me wonder if Wait For (job) JobDone(job As HttpJob) does not work while the app is not in the foreground. Any other reason why this request is not going through?