在我正在开发的游戏中,我想创建延迟。我造成的延迟是使用带有以下代码的处理程序:
handler.postDelayed(new Runnable() {
public void run() {
animationDrawable.start();
}
},theDelay);
问题是我有一个暂停按钮,它会让一切停止,并且在您再次恢复之前不执行任何操作。目前,即使游戏已暂停,它仍在运行。当我想暂停 Activity 时,我得到了一个改变的 bool 值。
我已经搜索了使用 Thread、TimerTask 和 Handler 的解决方案,但对于其中的每一个,我都看到了关于这些都不能安全使用的警告。
如何以安全的方式暂停和恢复此“倒计时”,而无需在恢复时重新开始?
最佳答案
How can I pause and resume this "countdown" in a secure way, and without having to start it all over again when resuming?
- 像这样启动处理程序时保存开始时间 -
startTime = System.currentTimeMillis();
- 在
onPause
内部保存该时间pauseTime = System.currentTimeMillis();
并取消hendler - 在
onResume
内部使用pauseTime - startTime
重新创建事件
关于android - 暂停和可恢复计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110277/