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

问题描述

| 我一直在尝试解决很多问题,尝试暂停和取消暂停计时器,如果我将方向锁定为纵向或横向,则可以使用,但这并不是我想要的。当然,在更改方向时会调用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,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,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\",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\");
}
}
    

解决方法

        您可以简单地添加一个布尔变量
boolean stopTImer = false ;
然后在您的timerTask中,执行以下操作:
@Overrride
public void run(){
if(!stopTimer){
//do stuff ...
//...
}
当您想停止时,将布尔值设为
true
    ,        您应该在onStop期间停止计时器。 Android可能会创建您的Activity的另一个实例,并且当您更改方向时,您将丢失对先前计时器(任务)的引用。 与活动相关联的所有对象都遵循活动生命周期。这意味着即使活动被删除(即使经常发生),也要保留对象的引用,就必须将它们存储在其他位置。