Android Question Delay in Time

Alhootti

Active Member
Hi guys

I'm using timer each 1000 ms will display time to label.

The issue is that after one month, the time is delayed by 2 minutes.

How do you get rid of this issue?

Note: I'm using an anderiod TV box integrated with battery 3 volts to save time.
 

Alhootti

Active Member
I'm using this code
could you please edit it.
B4X:
Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Timer1.Initialize("tmr",1000)
    Timer1.Enabled = True
End Sub

Sub tmr_tick
    DateTime.TimeFormat = "hh:mm:ss"
    Label1.Text = DateTime.Time(DateTime.Now)
End Sub
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
B4X:
    Label1.Text = DateTime.Time(DateTime.Now)

This is the correct code - you are reading the Android device time which should be UTC time maintained by an internet connection. If it shows an incorrect time it means that your Android TV box is incorrect. Where is your box getting its UTC reference from?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
I'm using this code
could you please edit it.
B4X:
Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Timer1.Initialize("tmr",1000)
    Timer1.Enabled = True
End Sub

Sub tmr_tick
    DateTime.TimeFormat = "hh:mm:ss"
    Label1.Text = DateTime.Time(DateTime.Now)
End Sub

1707651276271.png


B4X:
  DateTime.TimeFormat = "HH:mm:ss"

change hh to HH
 
Upvote 0

Alhootti

Active Member
B4X:
    Label1.Text = DateTime.Time(DateTime.Now)

This is the correct code - you are reading the Android device time which should be UTC time maintained by an internet connection. If it shows an incorrect time it means that your Android TV box is incorrect. Where is your box getting its UTC reference from?
The problem is there is no Internet connection somewhere.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
No. As long as you are getting the time using DateTime.Now then your app will not affect the clock precision.

It is possible that your app is leaking a resource and after a month it runs very slow.
Does the clock show the correct time after you restart the app?

Overall, mobile devices are not really built to run apps forever. It is a good practice to kill the app after 24 hours and restart it. You can do it with a second "watchdog" app.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Is there method to refresh the time every one hour programmatically to get ride this issue?
Well, you know that your device loses two minutes after one month, which is around two seconds every twelve hours. So in your timer tick events you could have a correction factor and increase it by two seconds every twelve hours (or one second every six hours) and add it to the reported DateTime(Now) value. It would not "get rid of the issue" because your device probably does not lose exactly two minutes every thirty days, but it would be a useful step forward.
 
Last edited:
Upvote 0
Top