Android - 如何停止和暂停计时器

3

我一直在尝试暂停和恢复计时器,但遇到了很多问题。如果我将方向锁定为纵向或横向,则可以正常工作,但这不是我想要做的。当然,改变方向时会调用onCreate方法,因此我取消了我的timertask并将其设置为null,但在多次更改方向后,它不再取消timertask。我查看了其他人在这里的问题,但没有一个问题能够解决我的问题。这是我的代码。目前有点混乱,因为我已经尝试了所有可能让它工作的方法。

public class singleTimer extends Activity implements OnClickListener {

private Integer setTime = 0;
private Integer tmrSeconds = 0;
private Integer tmrMilliSeconds = 0;
private Timer myTimer = new Timer();
private TimerTask myTimerTask;
private TextView timerText;
private boolean isPaused = true;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_timer);
    Bundle extras = getIntent().getExtras();
    setTime = extras.getInt("com.bv.armyprt.timer_duration");
    if (myTimerTask != null) {
        myTimerTask.cancel();
        myTimerTask = null;
    }
    if (savedInstanceState != null) {
        if (savedInstanceState.getInt("tmrSeconds") == 0) {
            tmrSeconds = setTime;
        } else {
            tmrSeconds = savedInstanceState.getInt("tmrSeconds");
            tmrMilliSeconds = savedInstanceState.getInt("tmrMilliseconds");

            if (isPaused == false) {
                myTimer = new Timer();
                myTimerTask = new TimerTask() {
                    @Override
                    public void run() {
                        TimerMethod();
                    }
                };
                myTimer.schedule(myTimerTask, 0, 100);
            }

        }
    } else {
        tmrSeconds = setTime;
    }
    timerText = (TextView)findViewById(R.id.timerText);
    timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));

    TextView timerDesc = (TextView)findViewById(R.id.timerDescription);
    timerDesc.setText("Timer for: " + setTime.toString());
    Button startButton = (Button)findViewById(R.id.timerStart);
    Button stopButton = (Button)findViewById(R.id.timerStop);
    Button closeButton = (Button)findViewById(R.id.timerClose);
    closeButton.setOnClickListener(this);
    startButton.setOnClickListener(this);
    stopButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case (R.id.timerStart):
        isPaused = false;
        myTimer = new Timer();
        myTimerTask = new TimerTask() {
            @Override
            public void run() {
                TimerMethod();
            }
        };
        myTimer.schedule(myTimerTask,0, 100);
        break;

    case (R.id.timerStop):
        isPaused = true;
        myTimerTask.cancel();
        myTimerTask = null;
        myTimer.cancel();

        break;

    case (R.id.timerClose):
        onDestroy();
        this.finish();
        break;
    }

}
private void TimerMethod()
{
    //This method is called directly by the timer
    //and runs in the same thread as the timer.
    //We call the method that will work with the UI
    //through the runOnUiThread method.
    this.
    tmrMilliSeconds--;
    this.runOnUiThread(Timer_Tick);
}

private Runnable Timer_Tick = new Runnable() {
    public void run() {

    //This method runs in the same thread as the UI.               
        if (tmrSeconds > 0) {
            if (tmrMilliSeconds <= 0) {
                tmrSeconds--;
                tmrMilliSeconds = 9;
            }
        } else {
            Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(1000);
            myTimer.cancel();
            tmrSeconds = setTime;
            tmrMilliSeconds = 0;
            isPaused = true;
        }

    //Do something to the UI thread here
        timerText.setText(String.format("%03d.%d", tmrSeconds, tmrMilliSeconds));
    }
};

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("setTimer", setTime);
    savedInstanceState.putInt("tmrSeconds", tmrSeconds);
    savedInstanceState.putInt("tmrMilliseconds", tmrMilliSeconds);

    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    setTime = savedInstanceState.getInt("setTimer");
    tmrSeconds = savedInstanceState.getInt("tmrSeconds");
    tmrMilliSeconds = savedInstanceState.getInt("tmrMilliSeconds");
}
}

当然,当您更改方向时,onCreate方法会被调用 - 您应该意识到这比那稍微复杂一些。当方向改变时,Activity会被完全销毁,然后重新创建。 - Squonk
2个回答

11

您可以简单地添加一个布尔变量

boolean stopTImer = false ;

在你的timerTask中,可以像这样做:

@Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}

当你想要停止它时,将布尔值设置为true


这就是我最终采取的做法,我使用了一个isPaused布尔变量,并在运行方法中检查它。感谢您的帮助。 - Shaun

2

在onStop期间应停止计时器。如果Android创建了该Activity的另一个实例并更改方向,则您将丢失对以前计时器(任务)的引用。

与Activity关联的所有对象都遵循Activity生命周期。这意味着,如果您希望在Activity被删除后保留这些对象(这种情况可能会经常发生),则必须在其他地方存储对象的引用。


那对我尝试做的事情部分有效,但我需要能够停止和恢复计时器。 - Shaun
这就是为什么你应该将计时器引用存储在更全局的位置。使用你的实现方式,如果活动被删除并重新实例化,则可能会丢失计时器引用。因为你在 onCreate 中暂停计时器(或设置暂停计时器),所以你不会暂停原始计时器而是新的计时器。你至少应该在 onStop 期间设置暂停标志,以防止原始计时器变得失控。 - marsbear

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接