Android Tutorial Tick Tock Test Timers Class

Works fine but the timer keeps cutting off in my rather big and complex app.... oh well I'll have to figure that out then.

Still perhaps useful to some.

You send it a number of milliseconds like 5000 for 5 seconds and it will call that timer t5000 then 5 seconds after it'll activate it and remove it from the queue.

You could send unique numbers like 5432 etc. to determine exactly which timer is being sent back to you.

I plan to use it in my app BTInterface so that the user can have their micro controller send a time like t10000 then after the appropriate time (ten seconds in this instance) BTInterface will send the string t10000 back again which the micro controller program will be looking out for to know when its time's up.

Just need to get it working with BTInterface :)

This first draft is very basic and not at all robust.
Perhaps someone can suggest improvements?

Incidentally, fun things happen if you accidentally use an Int instead of a Long so look out for that one if you have weird Timer issues.
 

Attachments

  • TickTockTest.zip
    8.3 KB · Views: 716
Last edited:

IanMc

Well-Known Member
Licensed User
Longtime User
Made it better like so:
B4X:
Sub Class_Globals
   Dim T As Timer
   Type NameAndTimeLong (Name As String, TimeLong As Long) 'The name of the timer and the long (milliseconds) of when its due
   Dim myTimers, deletes As List
   Dim tt As Boolean   
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   T.Initialize("T", 1000) 'obviously after testing you would reduce this number to give it a higher resolution
   myTimers.Initialize    ' and you'd remove all the logging and the Tick-Tocking etc. :)
   deletes.Initialize
   T.Enabled = True
End Sub

Sub T_tick
Try 
   If tt = True Then
      Log("tick")
      tt = False
   Else 
      Log("tock")
      tt = True
   End If
   
   For a = 0 To myTimers.Size -1
      Dim NameTime As NameAndTimeLong   
      NameTime = myTimers.Get(a)
      If NameTime.TimeLong < DateTime.Now Then
      Log("timer " & NameTime.Name)
         CallSub2(BTComms, "SendMsgBluetooth", NameTime.Name)
         deletes.Add(a)
      End If
   Next
   
   For a = 0 To deletes.Size -1
      myTimers.RemoveAt(deletes.Get(a)) 'can't be removing list items while iterating through the list :)
   Next
   deletes.Clear
Catch
   myTimers.Clear
   deletes.Clear
   Log("T_tick")
End Try
End Sub

Sub Add(inString As String)
Log("Adding " & inString)
   Dim s As String = inString.SubString(1).Trim
   If IsNumber(s) = True Then
      Dim NameTime As NameAndTimeLong   
      Log("it is the number " & s)
      NameTime.Name = inString.Trim
      NameTime.TimeLong = s + DateTime.Now
      myTimers.Add(NameTime)
      Log("time now is " & DateTime.Time(DateTime.Now))
      Log(DateTime.Time(NameTime.TimeLong))
      Log(myTimers.Size & " timer entries")
      T.Enabled = True
   End If
End Sub

It might be fun to make one that calls Subroutines?

Figured out why it was cutting off, I needed to initialise the object from my service, now it works fine.
 
Last edited:
Top