Hi
I have used the GPS example and added some functionality as the plan here is to have a simple GPS logger that submits it location every 1 minutes to a API using HTTP POST.
The app and service work fine when launched, and once the device screen is off through timeout or turned off however after about 6 minutes the app stops posting the JSON data, when the device is woken back up the data is submitted again. I added a count to the JSON and can see records 0-6 arrive fine then it stops, after waiting a few minutes and waking the device the data is received again with counts higher e.g. 10 showing that the service seems to still be working and counting but the data is not being sent.
I thought that the service was beign killed on my phone so I added #StartCommandReturnValue: android.app.Service.START_STICKY under the service attributes but no joy. After reading the forum and other examples I added StartServiceAt("", DateTime.Now + 60 * 1000, True) to schedule the restart, I believe the blank string is fine as it is scheduling itself.
As a side note I could not get Service.StartForeground to work at all as it always threw a complie error.
Here is some code from the service to show how it is working, any help would be great
I have used the GPS example and added some functionality as the plan here is to have a simple GPS logger that submits it location every 1 minutes to a API using HTTP POST.
The app and service work fine when launched, and once the device screen is off through timeout or turned off however after about 6 minutes the app stops posting the JSON data, when the device is woken back up the data is submitted again. I added a count to the JSON and can see records 0-6 arrive fine then it stops, after waiting a few minutes and waking the device the data is received again with counts higher e.g. 10 showing that the service seems to still be working and counting but the data is not being sent.
I thought that the service was beign killed on my phone so I added #StartCommandReturnValue: android.app.Service.START_STICKY under the service attributes but no joy. After reading the forum and other examples I added StartServiceAt("", DateTime.Now + 60 * 1000, True) to schedule the restart, I believe the blank string is fine as it is scheduling itself.
As a side note I could not get Service.StartForeground to work at all as it always threw a complie error.
Here is some code from the service to show how it is working, any help would be great
B4X:
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region
B4X:
Sub Service_Create
GPS1.Initialize("GPS")
StartServiceAt("", DateTime.Now + 60 * 1000, True)
End Sub
B4X:
Sub CreateJSON
'Create JSON file for sending
Dim gen As JSONGenerator
Dim m As Map
m.Initialize
m.Put("ID", srvDeviceID)
m.Put("Lat", srvLat)
m.Put("Lon", srvLon)
m.Put("Spd", srvSpeed)
m.Put("Ver", Version)
m.Put("Count", sendCount)
gen.Initialize(m)
genString = gen.ToString
If debugStatus = True Then
Log(genString)
End If
End Sub
B4X:
Sub PostLocation
'Set Data
srvLat = srvLocation.Latitude
srvLon = srvLocation.Longitude
Dim mph As Int = (srvLocation.Speed / 0.44704)
srvSpeed = mph
'Create JSON file for sending
CreateJSON
'Submit data
Dim NS As HttpJob
NS.Initialize("PostData",Me)
NS.PostString (sendURI, genString)
If debugStatus = True Then
Log("URL = " & sendURI & " String " & genString)
Log("Service Send")
Log("Send Number = " & sendCount)
End If
'Cleardown
lastSend = DateTime.Now
sendCount = sendCount + 1
End Sub
Sub JobDone (Job As HttpJob)
If debugStatus = True Then
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
End If
End Sub