暂停CountDownTimer在Android中,当活动是不是在前面在前面、CountDownTimer、Android

2023-09-12 09:40:39 作者:蓄满温柔.

我有一个使用CountDownTimer计数低于10如何暂停该计时器,当活动已不再是重点,比如如果用户接到一通电话什么的,然后恢复计时器,当用户进入一个活动回活动?这甚至可能?

I have an activity that uses a CountDownTimer that counts down from 10. How do I pause that timer when the activity is no longer in focus, like if the user get a call or something, then resume the timer when the user goes back to the activity? Is this even possible?

推荐答案

我想补充一些东西到onTick处理程序,以节省定时器的进步类(毫秒数左)。

I would add something to the onTick handler to save the progress of the timer in your class (number of milliseconds left).

在的onPause()方法,该活动调用取消()的定时器。

In the onPause() method for the activity call cancel() on the timer.

在onResume()方法的创建活动还剩下的毫秒数保存一个新的计时器。

In the onResume() method for the activity create a new timer with the saved number of milliseconds left.