Reputation: 2500
Here is my fixed rate timer code. Can i pause this timer while activity goes in onPause();
. If so then what would you suggest me put in onPause();
method and timer should start work as app comes to onResume();
:
//Declare the timer
t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// code here
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
20000,
//Set the amount of time between each execution (in milliseconds)
40000);
Upvotes: 1
Views: 705
Reputation: 69709
You can use Timer.cancel()
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.
Declare the timer as global
t = new Timer();
Try this
@Override
protected void onPause() {
super.onPause();
t.cancel();
}
timer should start work as app comes to onResume();:
You need to start Timer
in onResume()
Try this
@Override
protected void onResume() {
super.onResume();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// code here
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
20000,
//Set the amount of time between each execution (in milliseconds)
40000);
}
Upvotes: 1