Pause the timer and then continue it
If you have already canceled one timer, you can't re-start it, you'll have to create a new one.
See this answer, it contains a video and the source code how I did something similar.
Basically there are two method: pause and resume
In pause:
public void pause() {
this.timer.cancel();
}
In resume:
public void resume() {
this.timer = new Timer();
this.timer.schedule( aTask, 0, 1000 );
}
That makes the perception of pause/resume.
If your timers perform different actions based on the state of the application you may consider use the StatePattern
Fist define a abstract state:
abstract class TaskState {
public void run();
public TaskState next();
}
And provide as many states as you like. The key is that one state leads you to another.
class InitialState extends TaskState {
public void run() {
System.out.println( "starting...");
}
public TaskState next() {
return new FinalState();
}
}
class FinalState extends TaskState {
public void run() {
System.out.println("Finishing...");
}
public TaskState next(){
return new InitialState();
}
}
And then you change the state in your timer.
Timer timer = new Timer();
TaskState state = new InitialState();
timer.schedule( new TimerTask() {
public void run() {
this.state.run();
if( shouldChangeState() ) {
this.state = this.state.next();
}
}
}, 0, 1000 );
Finally, if what you need is to perform the same thing, but at different rates, you may consider using the TimingFramework. It is a bit more complex but let's you do cool animations, by allowing the painting of certain component take place at different rates ( instead of being linear )
TimerTask keeps running
Java is not exiting because your thread running the Timer is still kicking around. You have to mark that thread as being a daemon thread before Java will exit. You probably don't have access to the thread itself so unless Timer has a method to mark it so you'll have a hard time doing that. You'll need to manually stop it in a finally clause.
try {
timer = new Timer();
timer.schedule( new SysPrint(), 200 );
} finally {
timer.cancel();
}
How to pause, and resume a TimerTask/ Timer
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.
Pause TimerTask when android device is locked and resume back when it is unlocked
you can try below code
@Override
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn = powerManager.isScreenOn();
if (!isScreenOn) {
// The screen has been locked
// do stuff...
}
}
How to pause and resum a timer in Java
Try using a Swing timer.
private void createTimer() {
timer = new Timer(1000, (ae)->
{
timeElapsedInSeconds += 1;
System.out.println(
"Elapsed seconds: " + timeElapsedInSeconds);
timerSeconds.setText(
String.valueOf(timeElapsedInSeconds % 60));
timerMinutes.setText(String
.valueOf((timeElapsedInSeconds / 60) % 60));
timerHours.setText(String
.valueOf((timeElapsedInSeconds / 60) / 60));
});
timer.setDelay(1000);
timer.start(); // or start it elsewhere
}
Then you can use stop()
and start()
methods to pause and resume action. Check the javaDocs for more details.
Related Topics
How to Set Seekbar Min and Max Value
How to Navigate from One Screen to Another Screen
Android - How to Replace Part of a String by Another String
How to Get Difference Between Two Dates in Android, Tried Every Thing and Post
How to Implement "CSS Versioning" (To Solve Cache Issues) Using Jsf 2 H:Outputstylesheet
Different War Files, Shared Resources
Selenium Webdriver - Getcssvalue() Method
How to Use CSS for Vaadin Components
Javafx CSS Error ( Property Stylesheets Does Not Exist )
How to Generate a Qr Code for an Android Application
How to Give an Imageview Click Effect Like a Button on Android
Flutter Apps Are Too Big in Size
How to Change Package Name of an Android Application
When Is Onattach Called During the Fragment Lifecycle