暂停CountDownTimer在Android中,当活动是不是在前面 [英] Pause CountDownTimer in Android when activity is not in front

查看:426
本文介绍了暂停CountDownTimer在Android中,当活动是不是在前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用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.

这篇关于暂停CountDownTimer在Android中,当活动是不是在前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆