Timer for some code.


Whenever you don’t have time to have to program a massive for loop to keep track of things while you’re away programming something else you need a Timer. Danger’s OS has built in threading (allowing multiple things to happen at once) and a very simple implementation of it as well. To begin with determine how often you need your code to repeat. Timer’s work on a millisecond basis (1000 millisecond = 1 second) and begin and end at your beck and call.

Timer mTimer = new Timer(1000,true,this);

This code creates a timer that runs it’s Timer code every 1000 milliseconds (1 second). You can set a Timer up to repeat or run once by calling a boolean value (true in this case means this Timer runs it’s code every 1000 seconds until it stops). Each part of your program can have a Timer setup to run, you would declare which part of the program that will react to the timer in the last part of the call (in the earlier case we’re saying this part is waiting for the Timer – whatever this part is who knows).

So now you say, I’ve got a great Timer object that has started running, where do I keep all the cool stuff that it does once it actually finishes it’s big Timer countdown. I’ll show you exactly where to stick it…

public boolean receiveEvent(Event e) {
switch (e.type)
case Event.EVENT_TIMER:
Lots of code to do over and over again

Now you have a Timer that is doing something over and over; playing a sound, updating a StaticText object, counting down a clock, or just reminding you that you’re wasting second after second learning to program the world’s most advanced convergence device (the Danger Hiptop, but of course). Once you’ve finished doing all these things it’s simple to stop it in it’s tracks.

mTimer = null;

You don’t have to set the Timer to null but problems can arise if you create multiple timers that are named the exact same, they all start at the same time (making a Timer run 2x, 3x, or more times faster than normal).

Sample package zip file and Sample package bundle


No Responses Yet to “Timer for some code.”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: