Android Question sync app clock with system clock

Alessandro71

Well-Known Member
Licensed User
Longtime User
I need to refresh the main screen of my app (which is always on-screen by design) once a minute.
Actually I've implemented that with a 60 * 1000 period Timer.
The result is that the refresh does not happen when the system clock (shown in the notification bar) changes minute, but anytime after that.
What my be the most efficient way to sync with system clock?
I'd like to avoid a loop on DateTime.Now...
 
Solution
What my be the most efficient way to sync with system clock?

This seems to work:

B4X:
Sub Process_Globals
    Dim MinuteTimer As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MinuteTimer.Initialize("MinuteTimer", 123)    'arbitrary short initial timer period, to get clock initially displayed
    MinuteTimer.Enabled = True
End Sub

Sub MinuteTimer_Tick
    DateTime.TimeFormat = "HH:mm:ss.SSS"
    Log(DateTime.Time(DateTime.Now))
 
    MinuteTimer.Enabled = False
    MinuteTimer.Interval = 60000 - (DateTime.Now Mod 60000)    'number of ms until next minute rollover
    MinuteTimer.Enabled = True
End Sub
Log output:
Waiting for debugger to connect...
Program started.
18:40:51.067   'initial time is whenever program...

emexes

Expert
Licensed User
What my be the most efficient way to sync with system clock?

This seems to work:

B4X:
Sub Process_Globals
    Dim MinuteTimer As Timer
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MinuteTimer.Initialize("MinuteTimer", 123)    'arbitrary short initial timer period, to get clock initially displayed
    MinuteTimer.Enabled = True
End Sub

Sub MinuteTimer_Tick
    DateTime.TimeFormat = "HH:mm:ss.SSS"
    Log(DateTime.Time(DateTime.Now))
 
    MinuteTimer.Enabled = False
    MinuteTimer.Interval = 60000 - (DateTime.Now Mod 60000)    'number of ms until next minute rollover
    MinuteTimer.Enabled = True
End Sub
Log output:
Waiting for debugger to connect...
Program started.
18:40:51.067   'initial time is whenever program starts, probably not on minute rollover
18:41:00.061
18:42:00.007
18:43:00.009
18:44:00.010
 
Last edited:
Upvote 1
Solution
Top