Android Question Timers in Services

Reinierus

Member
Licensed User
Longtime User
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):

18:14:01
18:14:02
18:14:03
18:14:04
18:14:06
18:14:27
18:14:28
18:14:29
18:14:50
18:14:56
18:14:57
18:15:17
18:15:18
18:15:19
18:15:28
18:15:29
18:15:30
18:15:31
18:15:32
18:15:36
18:15:37

Note the jumps in the data

Thanks a lot
 

DonManfred

Expert
Licensed User
Longtime User
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...
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
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.

Any other idea?

Best regards
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
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.

Best regards
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
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.

Thanks
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
Hello Merlin.
I tried with PhoneWakeState PartialLock and works fine. I don't need to make the screen always visible.

Best regards
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
Ok, I'd need to start the service at every say 30 seconds (maybe user definable) and then insert records into a table and / or update the dashboard.
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
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

PD. Sorry for my bad English
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
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
 

Attachments

  • timedservice.zip
    6.7 KB · Views: 186
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

Reinierus

Member
Licensed User
Longtime User
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.
 
Upvote 0

merlin2049er

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0
Top