user1409534
user1409534

Reputation: 2304

Using CountDownTimer from background thread in Android

I want to use a CountDownTimer to update a text in UI thread from background thread that only counts time left.

I subclassed a thread and pass main thread handler in its constructor and creating a new CountDownTimer in run method to update text view in main ui thread as follows:

class MyCounter extends Thread
{
   private Handler uiHandler;
   private long timeToCount;

   public MyCounter(Handler handler, long time)
   {
     this.uiHandler = handler;
     this.timeToCount = time;
   }

public void run()
{
  new CountDownTimer(timeToCount, 1000){
       public void onTick(long millisUntilFinished) {
           Message msg =  Message.obtain();
           String timeString = //....here I conver millisUntilFinished to String format
           msg.obj = timeString;
           uiHandler.sendMessage(msg);
       }

      .... //Other method of CountDownTimer
     }.start();
  } //end of run
} //end of thread class

I create MyCounter thread and calling start on my activity UI (main thread) passing an handler created in main thread. I seems that CountDownTimer is not being called after calling start in my main activity.

Any ideas?

Upvotes: 1

Views: 3978

Answers (4)

SAM
SAM

Reputation: 122

new CountDownTimer(timeToCount, 1000){
    public void onTick(long millisUntilFinished) {
        runOnUiThread(new Runnable() {
            public void run()
            {
                Message msg =  Message.obtain();
                String timeString = //....here I conver millisUntilFinished to String format
                msg.obj = timeString;
                uiHandler.sendMessage(msg);
            }
       });
    }  
}.start();

Upvotes: 0

user1409534
user1409534

Reputation: 2304

O.K found my bug. I forgot to add Looper.loop() after calling start method on my CountDownTimer

Upvotes: 0

A-Droid Tech
A-Droid Tech

Reputation: 2321

I believe for this typical case, i.e. to run something with a fixed interval, Timer is more appropriate. Here is a simple example:

myTimer = new Timer();
myTimer.schedule(new TimerTask() {          
@Override
public void run() {
        // If you want to modify a view in your Activity
         MyActivity.this.runOnUiThread(new Runnable()
         public void run(){
              tv.append("Hello World");
         }  
       );
}
}, 1000, 1000); // initial delay 1 second, interval 1 second

Using Timer has few advantages: Initial delay and the interval can be easily specified in the schedule function arguments The timer can be stopped by simply calling myTimer.cancel() If you want to have only one thread running, remember to call myTimer.cancel() before scheduling a new one (if myTimer is not null)

Upvotes: 0

Yoganand.N
Yoganand.N

Reputation: 907

I think inorder to update a textview in activity u must use runOnUiThread eg:

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


                    textview1.setText(content);

        });

Upvotes: 3

Related Questions