这是我的固定汇率计时器代码。活动进入onPause();
时可以暂停计时器吗?如果是这样,那么您建议我在onPause();
方法中添加什么,并且当应用程序进入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);
答案 0 :(得分:1)
您可以使用Timer.cancel()
终止此计时器,丢弃任何当前计划的任务。不干扰当前正在执行的任务(如果存在)。计时器终止后,其执行线程将正常终止,并且无法在其上安排更多任务。
将计时器声明为全局
t = new Timer();
尝试一下
@Override
protected void onPause() {
super.onPause();
t.cancel();
}
定时器应在app进入onResume();时开始工作:
您需要在Timer
中启动onResume()
试试这个
@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);
}