倒数计时器

时间:2020-01-28 15:01:11

标签: java android

我正在尝试制作一个Pomodoro计时器(工作25分钟,休息5分钟,然后重复n次)。但是我找不到让中断计时器自动启动然后再使用工作计时器重新启动的方法(工作计时器= 00:10-中断计时器= 00:03,我输入了这些值以便可以更快地进行测试)。我该如何解决?

public class MainActivity extends AppCompatActivity {

TextView tvTimer, tvSessions;
ImageView ivStart, ivStop;
SeekBar seekBar;

CountDownTimer countDownTimer;

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

    tvTimer = findViewById(R.id.tvTimer);
    tvSessions = findViewById(R.id.tvSessions);
    ivStart = findViewById(R.id.ivStart);
    ivStop = findViewById(R.id.ivStop);
    seekBar = findViewById(R.id.seekBar);

    seekBar.setMax(2500);
    seekBar.setProgress(10);
    seekBar.setVisibility(View.GONE);

    tvTimer.setText("00:10");

}

// ON CLICK METHODS

public void startTimer(View v){
    countDownTimer = new CountDownTimer(seekBar.getProgress() * 1000 + 100, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            updateTimer((int) millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            breakTimer();
        }
    };

    countDownTimer.start();
}

public void stopTimer(View v){

}

// UPDATE METHODS

private void breakTimer(){
    seekBar.setProgress(3);
    tvTimer.setText("00:03");
}

private void updateTimer(int progress){
    int minutes = progress / 60;
    int seconds = progress - minutes * 60;

    String textMinutes = String.valueOf(minutes);
    String textSeconds = String.valueOf(seconds);

    if(seconds < 10) textSeconds = "0" + textSeconds;
    if(minutes < 10) textMinutes = "0" + textMinutes;

    tvTimer.setText(textMinutes + ":" + textSeconds);
}
}

预期输出:

00:10-> 00:00-> 00:03-> 00:00-> 00:10等等...

1 个答案:

答案 0 :(得分:3)

这样的事情怎么样。使用标记来跟踪计时器是休息计时器还是番茄计时器。

当计时器结束时,您检查标志以决定下一个计时器开始计时(并翻转标志以便下次执行相反的操作)。

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private static final int TICK_EVERY_SECOND = 1000;

    private TextView tvTimer, tvSessions;
    private ImageView ivStart, ivStop;
    private SeekBar seekBar;

    private CountDownTimer countDownTimer;

    private boolean isRest = false;
    private int userSelectedDurationSeconds = -1;

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

        tvTimer = findViewById(R.id.tvTimer);
        tvSessions = findViewById(R.id.tvSessions);
        ivStart = findViewById(R.id.ivStart);
        ivStop = findViewById(R.id.ivStop);
        seekBar = findViewById(R.id.seekBar);

        seekBar.setMax(2500);
        seekBar.setProgress(10);
        seekBar.setVisibility(View.GONE);

        tvTimer.setText("00:10");

    }

    // ON CLICK METHODS

    public void startTimer(View v) {
        isRest = false;
        int durationSeconds = seekBar.getProgress();
        userSelectedDurationSeconds = durationSeconds;
        restartTimer(durationSeconds);
    }

    private void restartTimer(int durationSeconds) {
        countDownTimer = new CountDownTimer(TimeUnit.SECONDS.toMillis(durationSeconds), TICK_EVERY_SECOND) {
            @Override
            public void onTick(long millisUntilFinished) {
                updateTimer((int) millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                breakTimer();
            }
        };

        countDownTimer.start();
    }

    public void stopTimer(View v) {

    }

    // UPDATE METHODS

    private void breakTimer() {
        isRest = !isRest;
        if(isRest) {
            seekBar.setProgress(3);
            tvTimer.setText("00:03");
            restartTimer(3);
        } else {
            restartTimer(userSelectedDurationSeconds);
        }
    }

    private void updateTimer(int progressInSeconds) {
        int minutes = progressInSeconds / 60;
        int seconds = progressInSeconds - minutes * 60;

        String textMinutes = String.valueOf(minutes);
        String textSeconds = String.valueOf(seconds);

        if (seconds < 10) textSeconds = "0" + textSeconds;
        if (minutes < 10) textMinutes = "0" + textMinutes;

        tvTimer.setText(textMinutes + ":" + textSeconds);
    }
}