简体   繁体   English

Android如何重启Timer Handler?

[英]Android how to restart Timer Handler?

I want to know how to restart a Timer after on Button click after timer has been stopped. 我想知道如何在计时器停止后单击按钮后重新启动计时器。 What I want is: 我想要的是:

  1. first button click: timer start 第一个按钮单击:计时器启动
  2. second click: timer stop 第二次点击:计时器停止
  3. third click: timer restart 第三次点击:计时器重启

The first two are working but not the restart. 前两个正在工作,但没有重新启动。

private Button startButton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

private int checkState = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pyramide);

    timerValue = (TextView) findViewById(R.id.pyramideTime);

    startButton = (Button) findViewById(R.id.pyramideButton);

    startButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // start
            if (checkState == 0) {
                startButton.setText("Fertig");
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);
                checkState = 1;
            }
            // pause
            else if (checkState == 1) {
                startButton.setText("Neu starten");
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
                checkState = 2;
            }
            // restart
            else if (checkState == 2) {
                timerValue.setText("0");
                timeInMilliseconds = 0L;

                customHandler.postDelayed(updateTimerThread, 0);
                checkState = 0;
                startButton.setText("Fertig");
            }
        }
    });

}

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":" + String.format("%02d", secs)
                + ":" + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};

You can try something like this using a counter : 您可以使用计数器尝试执行以下操作:

 startButton.setOnClickListener(new View.OnClickListener() {
                        int counter = 0;
    public void onClick(View view) {
                                        counter++;
                                    if (counter == 1) {
            startButton.setText("Fertig");
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
                                    else if (counter == 2) {
            startButton.setText("Neu starten");
            timeSwapBuff += timeInMilliseconds;
            customHandler.removeCallbacks(updateTimerThread);
                                    }
                                    else if (counter ==3) {
            timerValue.setText("0");
            timeInMilliseconds = 0L;

            customHandler.postDelayed(updateTimerThread, 0);
            startButton.setText("Fertig");
            counter = 0;
}
}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM