How to pause, and resume a TimerTask/ Timer

46,650

Solution 1

After a TimerTask is canceled, it cannot run again, you have to create a new instance.

Read details here:

https://stackoverflow.com/a/2098678/727768

ScheduledThreadPoolExecutor is recommended for newer code, it handles the cases like exceptions and task taking longer time than the scheduled interval.

But for your task, TimerTask should be enough.

Solution 2

Here's how I did it. Add pauseTimer boolean where ever the pause takes place (button listener perhaps) and don't count timer if true.

private void timer (){
    Timer timer = new Timer();
    tv_timer = (TextView) findViewById(R.id.tv_locationTimer);
    countTimer = 0;
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String s_time = String.format("%02d:%02d:%02d",
                            countTimer / 3600,
                            (countTimer % 3600) / 60,
                            countTimer % 60);
                    tv_timer.setText(s_time);
                    if (!pauseTimer) countTimer++;
                }
            });
        }
    }, 1000, 1000);
}

Solution 3

For Kotlin user, checkout this

How to use:

// Init timer
lateinit var timerExt: CountDownTimerExt

timerExt = object : CountDownTimerExt(TIMER_DURATION, TIMER_INTERVAL) {
    override fun onTimerTick(millisUntilFinished: Long) {
        Log.d("MainActivity", "onTimerTick $millisUntilFinished")
    }

    override fun onTimerFinish() {
        Log.d("MainActivity", "onTimerFinish")
    }

}

// Start/Resume timer
timerExt.start()

// Pause timer
timerExt.pause()

// Restart timer
timerExt.restart()
Share:
46,650

Related videos on Youtube

embersofadyingfire
Author by

embersofadyingfire

Updated on July 09, 2022

Comments

  • embersofadyingfire
    embersofadyingfire almost 2 years

    I have an animation in my Android app that flashes a TextView different colors. I've used a TimerTask, Timer, and Runnable method to implement this. What I need to do is stop the thread when a user leaves the app during this animation in onPause(), and resume the thread when the user returns to the app in onResume(). The following is the code I've implemented, but it's not working (the onPause(), and onResume() pieces), and I don't understand why. I've read a few other posts on similar matters, but they haven't helped me figure out what to do in my situation. I've read that TimerTasks are outdated, and I should probably use an ExecutorService method; it is unclear to me as how to implement this function.

       ...timerStep5 = new TimerTask() {
    
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                    if (b5) {
                        cashButton2SignalText.setBackgroundColor(Color.RED);
                        cashButton2SignalText.setTextColor(Color.WHITE);
                        b5=false;
                    } else {
                        cashButton2SignalText.setBackgroundColor(Color.WHITE);
                        cashButton2SignalText.setTextColor(Color.RED);
                        b5=true;
                    }
                    }
                });
            }
    };
    
    timer5.schedule(timerStep5,250,250);
    
    }
    
    public void onPause(){
    
        super.onPause();
    
        timerStep5.cancel();
    
    }
    
    public void onResume(){
    
        super.onResume();
    
        timerStep5.run();
    
    }
    
  • embersofadyingfire
    embersofadyingfire about 11 years
    Ok, I saw this post earlier, and was confused with it; maybe you can clarify something for me: a user might leave my app during the TimerTask activity, and return to that activity more than once. Wouldn't that mean I would have to create a new instance of a TimerTask within onResume() every time the user leaves during this activity, and returns?
  • embersofadyingfire
    embersofadyingfire about 11 years
    Question: If I were to ignore the action of a user leaving this activity for a moment and returning, and left out any implementation for pausing and resuming the TimerTask, but implemented onStop() to cancel the TimerTask when the user finally moves forward to another activity, would this cause some kind of memory leak while the TimerTask activity is paused?
  • X.Y.
    X.Y. about 11 years
    I don't think there will be any memory leak, but it does not make sense to let your UI updating task run in background for nothing. Yes, you're supposed to create TimerTask on the fly and it's not reusable. It's designed this way. (I guess for thread-safety) Also onResume is not some tight loop, the cost of creating an object is negligible.