Waffles
Waffles

Reputation: 349

Why is my countdown timer in Android so slow?

I'm trying to make a countdown timer in android for use in a small android app. The app will countdown from some number of seconds to 0, upon which it will do some action. I'm using the coundowntimer supplied by android.os.countdowntimer. Here is my code:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quizlayout);

    new CountDownTimer(30000, 1000) {

        TextView tx = (TextView) findViewById(R.id.textView2);

         public void onTick(long millisUntilFinished) {
             tx.setText("seconds remaining: " + millisUntilFinished / 1000);
         }

         public void onFinish() {
             tx.setText("done!");
         }
      }.start();
}

However, this countdown timer is really slow. It takes like 3 real-time seconds for the timer to countdown by one second. I wonder what's going on? The code I have above is more or less copied straight from google (CountDownTimer)

Can anyone help me as per why my timer is so slow, and offer a way to speed it up a bit?

(EDIT): I am running this on an emulator, the intel atom x86. I am emulating an android 2.3.3 environment.

Upvotes: 0

Views: 3330

Answers (3)

Samet
Samet

Reputation: 917

CountDownTimer is not efficient regardless to ui updating performances. For a flawless ui update, it is better to create a custom countdown. I did my own so here it is. It is flawless on my app.

public abstract class CountDown {

int totalTime = 0;
int tickTime = 0;

Thread thread;
boolean canceled = false;

public CountDown(int totalTime,int tickTime){
    this.totalTime = totalTime;
    this.tickTime = tickTime;
}

public abstract void onTick();

public abstract void onFinish();

public void start(){

    thread = new Thread(new Runnable() {

        @Override
        public void run() {
            // Do in thread

            canceled = false;

            for (int elapsedTime = 0; elapsedTime < totalTime; elapsedTime += tickTime) {

                if(!canceled){

                        onTick();
                        try {
                            thread.sleep(tickTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                }else{
                    break;
                }
            }

            if(!canceled){
                onFinish();
            }

        }

    });

    thread.start();
}

public void cancel(){
    canceled = true;
}
}

Remember that every time you have to update your ui, call a runOnUiThread, or else you will have an exception, you are not in a handler and not on ui thread. Here is how to use it in your code, it is identical to CountDownTimer, so you could just rename lines in your code :

   CountDown cDown = new CountDown(10000, 20) {

        public void onTick() {

        // Do something

        }

        public void onFinish() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    myButton.setImageDrawable(drawable);

                }
            });

        }
    };

Upvotes: 1

android developer
android developer

Reputation: 116342

use a handler that will post the same runnable . this will remove the need for extra threads :

Handler handler=new Handler();
handler.postRunnable(... , 1000) ;

in the runnable , call the postRunnable again for the same handler (and add a condition for when to stop) .

Upvotes: 1

Orlymee
Orlymee

Reputation: 2357

According to Android documentation for countdown timer

The calls to onTick(long) are synchronized to this object so that one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval.

Take a look at this example for countdown timer Countdown timer example

Alternately you can spawn a new thread and just get that thread to sleep for the interval you want and take actions when it wakes or vice versa.

You can also timertask

Upvotes: 1

Related Questions