Android Question Timers

Paul Boyle

Member
Licensed User
Longtime User
Hi

I have an app with multiple games, each with a button the main activity/page. One requires a timer, so that when this games button (eg. btn_MyGame) is pressed it starts that game's activity and the timer is started.

I'm wondering how to start the timer when btn_MyGame is pressed and show the time elapsed on a label within the MyGame activity.

So after 3 mins of playing MyGame the label would read: Session Time 0h 3m

I know very little about timers (obviously).

Any help appreciated.

Many Thanks.

P.
 

Paul Boyle

Member
Licensed User
Longtime User
What data type is the variable for storing the current time.

Dim Timer01 as Timer
-------------------------------

If FirstTime = True Then
Timer01.Initialize("Timer01",1000)
End If

---------------------------------

If Timer01.Enabled = False Then
Timer01.Enabled = True
End If
----------------------------------

Dim StartTime as ???????

Dim GameTime as ??????

GameTime = DateTime.Now - StartTime
 
Upvote 0

Paul Boyle

Member
Licensed User
Longtime User

Thanks

Anyone got any handy code for displaying Hrs, mins & secs in a label.


This errors out.

DateTime.TimeFormat = "HH:mm:ss"

lng_GameTime = DateTime.Now - lng_StartTime01
lng_GameTime =DateTime.Time(lng_GameTime)

lbl_Timer.Text = lng_GameTime


Or is it better to use the Mod math function?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
What was the error message ?? majority of times it will point directly to the problem .

try this ..
B4X:
Dim lngStartTime, lngEndTime, lngGameTime As Long  
lngStartTime = DateTime.Now
'.............
'...... game over
lngEndTime = DateTime.Now + 62000 '...  @@@ for example only ! .. add 1m:02sec)
lngGameTime = lngEndTime - lngStartTime

DateTime.TimeFormat = "HH:mm:ss"  
Log (DateTime.Time(lngGameTime))
 
Upvote 0

Paul Boyle

Member
Licensed User
Longtime User
Thanks a lot. I have the below code which works but for one thing.

B4X:
Sub Timer01_Tick
   
    lng_GameTime = DateTime.Now - lng_StartTime01
   
    DateTime.TimeFormat = "HH:mm:ss"
   
    lbl_Timer.Text=  DateTime.Time(lng_GameTime)
   

End Sub

lbl_Timer.Text starts up as 01:00:00 instead of 00:00:00, which is probably down to summer/winter time zone. Is there a way to determine if its +/- 1hr ?

Thanks.
 
Upvote 0
Top