gpt4 book ai didi

android - 即使 Activity 停止,计时器也会运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:49:29 25 4
gpt4 key购买 nike

已从 onCreate 方法启动我的计时器。即使我的 Activity 停止,计时器也会继续运行。怎么可能?

Toast在onTick()方法中显示,Thread是主线程。我想,当 Activity 停止时,主线程将停止。任何人都可以对此进行解释。

CustomCountDownTimer.java

private class CustomCountDownTimer extends CountDownTimer
{
public CustomCountDownTimer(long millisInFuture,long countDownInterval)
{
super(millisInFuture,countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub

long seconds=millisUntilFinished/1000;
long hours=seconds/(60*60);
long remainingSeconds=seconds%(60*60);
long minutes=remainingSeconds/60;
remainingSeconds=remainingSeconds%60;

Toast.makeText(HomeActivity.this, Thread.currentThread().getName()+"", Toast.LENGTH_LONG).show();


btnStatusMessage.setText(hours+" hr "+minutes+" min ");

}

最佳答案

计时器与 Activity 无关。您需要在 Activity 的状态方法之一(onPause/onStop/etc')中停止它。

关于android - 即使 Activity 停止,计时器也会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19443057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com