Hello.
I have a service with a timer at 1000 (1 second). The event of the timer stores data in a local database. But in the data I see records no always in a second, for example, every 10 seconds, 30 seconds. This is part of my code:
B4X:
Sub Service_Create
Timer1.Initialize("Timer1", 1000)
Timer1.Enabled = True
End Sub
B4X:
Sub Timer1_Tick
If vrGrabaDatos=True Then
vrSQL.ExecNonQuery("INSERT INTO Estadisticas VALUES(NULL, '" & vrLatitud & "' , '" & vrLatitudDiff & "' , '" & vrLongitud & " ', '" & vrLongitudDiff & "' , " & vrVelocidadKM & "," & vrDistanciaKM & ", '" & DateTime.Date(DateTime.Now) & "','" & DateTime.Time(DateTime.Now) & "')")
End If
End Sub
And the result in the database "Estadisticas" is (the "DateTime.Time(DateTime.Now)" field):
vrGrabaDatos seems to be false when the timer tick event starts. So no db-entry is written in this sub...
Check why vrGrabaDatos is false at this place.
You can put log-commands to log the time the timer tick is called to know whether the timer runs correctly...
Hello DonManfred.
vrGrabaDatos is always in "True" for record the data. It is the signal to start recording and, when is "False", it stops recording. If vrGrabaDatos is "False", there is not data at all.
like i wrote... Put log´s inside the sub to SEE the sub is working every second (or even not)... If there are unexpected gaps then you should search about infos why the timer is not running every second...
Thanks DonManfred.
I believe that the service is not responding because on it is also running the GPS events. I think is better to use a different service only for the timer. I will try it and let you know.
Hello DonManfred
I tried with another service, but I got the same result, so I will try with PhoneWakeState, first with PartialLock and, if doesn't work, with KeepAlive. I will let you know.
I will need to see your code, but as you say, I think you need one service with a timer running every 30 seconds and makes it to record and/or the update. When the service is not recording or updating, it can be waiting.
To make the phone always awake:
B4X:
'In Globals
Dim phoneDespierto As PhoneWakeState
'In Activity_Create
phoneDespierto.Partiallock
'In the end app button or end sub
phoneDespierto.ReleasePartialLock
It´s up to you to make the time between defineable... See an simple service-example
when started it runs every 5 seconds and log a randum number...
B4X:
Sub Service_Start (StartingIntent As Intent)
Log(DateTime.Time(DateTime.Now)&": Service_Start")
' Do what ever you need to do here. Insert things into db, whatever
Dim r As Int = Rnd(100,200)
Log(DateTime.Time(DateTime.Now)&": random="&r)
StartServiceAt("", DateTime.Now + 5 * 1000, False)
StopService("")
End Sub
Thanks, that will be really helpful. I've got a dashboard in my main activity, can I call it from the timer or do I need to move that to the service. I wasn't too sure about this, I guess I can try it both ways.
The dashboard is a group of views? I believe you cannot load views from services. If I need to show services variables in views, I put a timer in the activity and the variable in the Process_Globals on the service, so, every short time the activity (timer) ask for that variable and show it. I don't know if there is a best way to do that, but works for me.
I don´t know if I understand you well.
My dashboard is one panel in the main activity that's going to monitor 5 variables, and a button which enables / disables the timer service.
So, hopefully in the timer service I can set it to recreate the service every 30 seconds or so, and read the bluetooth variables and populat the dashboard and insert the variables into a table.