Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to change a TimerTask interval after shcedule?

Tags:

java

android

timerUploadTime = new Timer();
timerUploadTimeTask = new TimerTask() {
   @Override public void run() {
      mHandler.post(new Runnable() {
        @Override
         public void run() {
             ...
          }
      }
    });
 }
 };

timerUploadTime.schedule(timerUploadTimeTask, 1, Integer.parseInt(Utils.loadStringValue(mycontext, "refresh")));

I'd like to change my timer's interval, in an other code segment. Is there a way to do that? I dont want to retiming my code, i would like to add a new "period" interval.

How can I do that after schedule?

like image 942
lacas Avatar asked Aug 31 '11 13:08

lacas


2 Answers

You can't. You'll have to cancel the current task and reschedule.

like image 138
mre Avatar answered Oct 19 '22 18:10

mre


You should cancel the timer before you can set a new schedule to it.

timerTask.cancel();
timerTask = new MyTimerTask();
timer.schedule(timerTask, delay);
like image 38
Ronnie Avatar answered Oct 19 '22 18:10

Ronnie