Java – pause the timer and continue
Please refer to the code posted by @ Yuri here How to stop a timer after certain number of times. If I want to stop it because of some conditions, then restart it What should I do?
private final static int DELAY = 10000; private final Handler handler = new Handler(); private final Timer timer = new Timer(); private final TimerTask task = new TimerTask() { private int counter = 0; public void run() { handler.post(new Runnable() { public void run() { Toast.makeText(MainActivity.this,"test",Toast.LENGTH_SHORT).show(); } }); if(++counter == 4) { timer.cancel(); } //do some stuff in my app //restart the timer again } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timer.schedule(task,DELAY,DELAY); }
This is what I tried, but it keeps collapsing
final int DELAY = 10000; Timer timer; MyTask task; startManager Scanner; Handler handler; public class MyTask extends TimerTask { @Override public void run() { handler.post(new Runnable() { public void run() { //do Stuff here } }); } public class startManager { public startManager() { handler = new Handler(); timer = new Timer(); } public void start() { timer.schedule(task,DELAY); } public void cancel() { timer.cancel(); timer.purge(); } } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Scanner = new startManager(); //do some stuff if (...) Scanner.cancel() //restart the timer and its task Scanner=new startManager(); }
Solution
If you have cancelled a timer, you cannot restart it. You must create a timer
See this answer, which contains a video and source code. How did I do something similar
There are basically two methods: pause and resume
Pause:
public void pause() { this.timer.cancel(); }
In your resume:
public void resume() { this.timer = new Timer(); this.timer.schedule( aTask,1000 ); }
This makes the feeling of pause / recovery
If your timer performs different operations based on the state of your application, consider using statepattern
Define abstract state:
abstract class TaskState { public void run(); public TaskState next(); }
And provide as many states as possible The key is that one country 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(); } }
Then change the status in the 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(); } } },1000 );
Finally, if you need to do the same thing but at different speeds, you can consider using the timing framework It's a little complicated, but let's do some cool animation by allowing some components to be painted at different speeds (not linear)