我如何prevent从后台运行我CountDownTimer?后台、prevent、CountDownTimer

2023-09-06 22:42:43 作者:别撩脸子i

我想提出一个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

This答案可能是有帮助的,如果​​你需要传递的价值,另一个活动

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

 
精彩推荐
图片推荐