Android Question [solved] I need to create a Timer () that runs every 80 seconds

Sergio Castellari

Active Member
Licensed User
Clarification: Never use a Timer ()
1) I need to create a Timer () that executes an "update" of a MySQL table (through jRDC2) every 80 seconds during the entire life cycle of the application
2) Any basic code example?
Greetings
 

Peter Simpson

Expert
Licensed User
Longtime User
Well you could
  1. Use StartServiceAt in a service, set it to run at the current time + 80000 milliseconds
  2. Use a timer in a service, this is not as stable though
Don't forget to add the following lines at the bottom of Service_Start
B4X:
    Sleep(0)
    Service.StopAutomaticForeground
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Well you could
  1. Use StartServiceAt in a service, set it to run at the current time + 80000 milliseconds
  2. Use a timer in a service, this is not as stable though
Don't forget to add the following lines at the bottom of Service_Start
B4X:
    Sleep(0)
    Service.StopAutomaticForeground
Hi @Peter Simpson,
Excuse my ignorance, but I still don't understand where I should put the Timer ().
I started the project as B4XPages.
Where's the service ?
Thank you again in advance
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Upvote 0

Sergio Castellari

Active Member
Licensed User
Nothing special is needed. If you are using B4XPages then put it in any page you like, otherwise put the timer in the starter service.
Brilliant!!!
@Erel, I need a little concrete example.
I don't realize where the start service is on the "Main".
I imagine it is something very easy to do, but for my first time I can't understand it.
From already thank you very much
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Because you said the above I presumed that you were using a service and that the app was running in the background...
@Peter Simpson
I am a beginner, I never run a service and I think that for this case I do not need it.
What I require is an example of code with a Timer (), which runs every 80 seconds during the life cycle of the APP.
This Timer () will simply run a jRDC2 command that updates a DateTime field.
Greetings and thanks!
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Put this in B4XMainPage.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim Timer1 As Timer
    Dim Interval As Int = 10
    Dim Counter As Int
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ' Start a timer
    Timer1.Initialize("Timer1", Interval * 1000)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
    Log(" ")
    Log("Run a database update query")
    Counter = Counter + 1
    'AddRecord("Insert #" & Counter)
    UpdateID("Updated #" & Counter, 1)
    Log("Next run " & DateTime.Time(DateTime.Now + Interval * 1000))
End Sub

'Sub AddRecord(Message As String)
'    Dim req As DBRequestManager = CreateRequest
'    Dim cmd As DBCommand = CreateCommand("insert_test", Array(Message))
'    Wait For (req.ExecuteCommand(cmd, Null)) JobDone(j As HttpJob)
'    If j.Success Then
'        LogDebug("Success")
'    Else
'        Log("Error: " & j.ErrorMessage)
'    End If
'    j.Release
'End Sub

Sub UpdateID(Message As String, ID As Int)
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand("update_test", Array(Message, ID))
    Wait For (req.ExecuteCommand(cmd, Null)) JobDone(j As HttpJob)
    If j.Success Then
        LogDebug("Success")
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub

Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, Main.rdcLink)
    Return req
End Sub

Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub

Change the Interval for 80 seconds.
B4X:
Dim Interval As Int = 80

Config.properties
B4X:
sql.insert_test=INSERT INTO test (message, updated_on) VALUES (?, now())
sql.update_test=UPDATE test SET message = ?, updated_on = now() WHERE id = ?

Table schema (MySQL)
SQL:
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `updated_on` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 
Last edited:
Upvote 0

RodM

Member
Licensed User
Brilliant!!!
@Erel, I need a little concrete example.
I don't realize where the start service is on the "Main".
I imagine it is something very easy to do, but for my first time I can't understand it.
From already thank you very much
Hi Sergio,

If I understood it right, as Erel said, you are using B4XPages, so you can put it in any page you like.
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Put this in B4XMainPage.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim Timer1 As Timer
    Dim Interval As Int = 10
    Dim Counter As Int
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    ' Start a timer
    Timer1.Initialize("Timer1", Interval * 1000)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
    Log(" ")
    Log("Run a database update query")
    Counter = Counter + 1
    'AddRecord("Insert #" & Counter)
    UpdateID("Updated #" & Counter, 1)
    Log("Next run " & DateTime.Time(DateTime.Now + Interval * 1000))
End Sub

'Sub AddRecord(Message As String)
'    Dim req As DBRequestManager = CreateRequest
'    Dim cmd As DBCommand = CreateCommand("insert_test", Array(Message))
'    Wait For (req.ExecuteCommand(cmd, Null)) JobDone(j As HttpJob)
'    If j.Success Then
'        LogDebug("Success")
'    Else
'        Log("Error: " & j.ErrorMessage)
'    End If
'    j.Release
'End Sub

Sub UpdateID(Message As String, ID As Int)
    Dim req As DBRequestManager = CreateRequest
    Dim cmd As DBCommand = CreateCommand("update_test", Array(Message, ID))
    Wait For (req.ExecuteCommand(cmd, Null)) JobDone(j As HttpJob)
    If j.Success Then
        LogDebug("Success")
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release
End Sub

Sub CreateRequest As DBRequestManager
    Dim req As DBRequestManager
    req.Initialize(Me, Main.rdcLink)
    Return req
End Sub

Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
    Dim cmd As DBCommand
    cmd.Initialize
    cmd.Name = Name
    If Parameters <> Null Then cmd.Parameters = Parameters
    Return cmd
End Sub

Change the Interval for 80 seconds.
B4X:
Dim Interval As Int = 80

Config.properties
B4X:
sql.insert_test=INSERT INTO test (message, updated_on) VALUES (?, now())
sql.update_test=UPDATE test SET message = ?, updated_on = now() WHERE id = ?

Table schema (MySQL)
SQL:
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `updated_on` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Thank you very much @aeric
I understood how Timer () works, when to initialize it and when to activate it.
His example was fabulous!
Big hug!
 
Upvote 0
Top