Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause, and resume a TimerTask/ Timer

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();

}
like image 422
embersofadyingfire Avatar asked Mar 15 '13 22:03

embersofadyingfire


People also ask

Can you pause a timer in Java?

Try using a Swing timer. Then you can use stop() and start() methods to pause and resume action.

How do you pause and resume activity on Android?

As your activity enters the paused state, the system calls the onPause() method on your Activity , which allows you to stop ongoing actions that should not continue while paused (such as a video) or persist any information that should be permanently saved in case the user continues to leave your app.


2 Answers

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.

like image 79
X.Y. Avatar answered Oct 06 '22 22:10

X.Y.


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);
}
like image 4
seekingStillness Avatar answered Oct 06 '22 22:10

seekingStillness