我如何prevent从后台运行我CountDownTimer? [英] How do I prevent my CountDownTimer from running in background?

查看:272
本文介绍了我如何prevent从后台运行我CountDownTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个Android应用程序,有一个时间限制来分的,你可以得到的金额。但是,如果你关闭该应用程序,计时器不断前进。如何暂停了 CountDownTimer 当应用程序暂停?

I am making an Android app that has a time limit to the amount of points you can get. But if you close the app, the timer keeps going. How do I pause the CountDownTimer when the app pauses?

推荐答案

您可以在的onPause取消()的东西,如

@Override
public void onPause()
{
    super.onPause();
    timer.cancel();    // timer is a reference to my inner CountDownTimer class
    timer = null;
}

和使用 millisUntilFinished 变量保存在共享preference 或一些其他持久性变量。然后使用该变量再次启动 onResume计时器()

And use the millisUntilFinished variable to save in a SharedPreference or some other persistent variable. Then use that variable again to start the timer in onResume()

共享preFS

<一个href="http://stackoverflow.com/questions/27130057/passing-countdown-timer-beetwen-activities-in-android/27130218#27130218">This答案可能是有帮助的,如果​​你需要传递的价值,另一个活动

This answer may be helpful if you need to pass the value to another Activity.

这篇关于我如何prevent从后台运行我CountDownTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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