我的Android应用程序中有一个动画,它使TextView呈现不同的颜色。我使用了TimerTask,Timer和Runnable方法来实现这一点。我需要做的就是当用户在onPause()这个动画中离开应用程序时停止线程,并在用户返回到onResume()中的应用程序时恢复线程。以下是我实现的代码,但它不工作(onPause()和onResume()件),我不明白为什么。我已经阅读了其他一些关于类似问题的文章,但他们并没有帮助我找出在我的情况下做什么。我读过TimerTasks已经过时了,我应该使用一个ExecutorService方法;我不清楚如何实现这个功能。如何暂停和恢复TimerTask /定时器
...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();
}
[暂停/停止和启动/恢复持续的Java的TimerTask?]的可能的复制(http://stackoverflow.com/questions/2098642/pausing-stopping-and-starting-resuming-java-timertask-continuously ) – Gboy 2017-02-11 12:29:12