Countdown or Runnable and Handler, what’s the difference?

Today in class, we learned about different types of timers. Specifically, we learned the difference between a countdown timer, and a runnable and handler. Here’s the app that I put together based on the class:

http://www.mediafire.com/file/0xgqkkbqjaztjmw/thefinalcountdown.apk

It’s not very exciting, hense no screenshot. It has three different toast messages going on at the same time. The first is a toast every seven seconds to tell you it has been seven seconds. The second is a toast every ten seconds telling you how much time is left on the countdown timer, and the third tells you that the countdown is complete.

Here is the Handler and runnable:

[CODE]

// Up first, the Handler and runnable!

// Create the handler, give it a name.
final Handler firstHandler = new Handler();

// Now create a runnable, give it a name.
Runnable firstRun = new Runnable() {

// Now, have that runnable override and run some code.
@Override
public void run() {

// In this case, make a toast.
Toast myToast = Toast.makeText(getApplicationContext(), “It’s been 7 seconds.”, Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.CENTER, 0, 0);
myToast.show();

// And call itself again in 7 seconds….
firstHandler.postDelayed(this, 7000);

}

};

// Be sure to initiate the handler the first time, or nothing will happen.
// You could just use firstHandler.post(firstRun); but I wanted to wait seven seconds first.
firstHandler.postDelayed(firstRun, 7000);
[/CODE]

And here is the countdown timer:

[CODE]

// Up next, the countdown timer! Give it a lenght and an amount to count down in milliseconds.
new CountDownTimer(121000, 10000) {

// Implement code to happen every tick of the countdown.
public void onTick (long myTimer) {

// In this case a toast.
Toast.makeText(
getApplicationContext(), “Ten seconds passed, ” + String.valueOf(myTimer / 1000) + ” seconds left!”, Toast.LENGTH_SHORT).show();

}

// Implement code to happen when it is done counting down.
public void onFinish() {

// Again, I used a toast.
Toast.makeText(getApplicationContext(), “Countdown is complete!”, Toast.LENGTH_SHORT).show();

}

// Tell the countdown timer to start!
}.start();

}
[/CODE]

So, what’s the difference? Well, from what Rob was saying, the key difference is that the Countdown timer and Handler/Runnable can do the exact same thing. However, the Countdown timer has a way of destroying itself when it is done.

At first I thought, “oh, the countdown timer will stop eventually, and the handler/runnable will not”. However, that is an error, because we could make the handler/runnable only display x number of times, by using a variable and minus 1 from it every time.
Like this:

[CODE]
…..

public int a = 10;

…..

// Create the handler, give it a name.
final Handler firstHandler = new Handler();

// Now create a runnable, give it a name.
Runnable firstRun = new Runnable() {

// Now, have that runnable override and run some code.
@Override
public void run() {

// In this case, make a toast.
Toast myToast = Toast.makeText(getApplicationContext(), “It’s been 7 seconds.”, Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.CENTER, 0, 0);
myToast.show();

a = a-1;

if (a >= 0) {

// And call itself again in 7 seconds….
firstHandler.postDelayed(this, 7000);

} else {
// do nothing, or a toast that says “I’m done.”
}

}

};

// Be sure to initiate the handler the first time, or nothing will happen.
// You could just use firstHandler.post(firstRun); but I wanted to wait seven seconds first.
firstHandler.postDelayed(firstRun, 7000);
[/CODE]

Once it reaches 0, it could use an if/then statement to not call itself to run again. So, the two can litterally function the same, it is just a matter of being “left over” after you are done with it. Interesting.

So, the pros to using a handler/runnable is that you can call it from other functions, and it can run on indefinately.
And, a pro for using the countdown timer is that you can easily get the remaining time for display or use as a variable.

Perhaps I will learn new differences that may make one more prominent than the other. Only time will tell. Okay, bad pun, bad pun….

Linux – keep it simple.

 

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s