0

I have spent the evening trying to implement a pause/resume button for my countdown timer. I thought I had it all working until after pausing and resuming the countdown timer failed to start my next countdown timer.

In my code I have a timer that has 3 different durations (1 for "Get Ready", "Hang" and "Rest") which begin after the previous timer hits 0. This cycle repeats 10 times (for the 10 sets) then ends.

What would be the best way to implement a pause/resume button that would work across all 3 of my countdown durations and resumes the time from its current position after pausing? Any and all suggestions, example code would be greatly appreciated.

I did end up removing the pause/resume button for now. I am very new to any kind of coding and wanted to see how far I could get before I needed some help.

Thanks.

public class TimerFragment extends Fragment {

    private static final long COUNTDOWN_DURATION = 7100; // 7 seconds in milliseconds
    private static final long REST_DURATION = 111000; // 1 minute 50 seconds in milliseconds
    private static final int NUM_SETS = 10;
    private boolean mIsTimerRunning = false;
    private int mCurrentSet = 1;
    private TextView mTextView;
    private Button mButton;
    private CountDownTimer mTimer;


    public TimerFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_timer, container, false);
        mTextView = view.findViewById(R.id.timerTextView);
        mButton = view.findViewById(R.id.buttonStart);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIsTimerRunning) {
                    stopTimer();
                } else {
                    startTimer();
                }
            }
        });
        return view;
    }

    private void startTimer() {
        mIsTimerRunning = true;
        mButton.setText(R.string.stop);

        // Starts the "GET READY" countdown.
        mTimer = new CountDownTimer(11000, 1000) {
            public void onTick(long millisUntilFinished) {
                mTextView.setText(String.valueOf(millisUntilFinished / 1000));
                TextView statusTextView = getView().findViewById(R.id.statusTextView);
                statusTextView.setText("준비");
            }

            public void onFinish() {
                // Starts the "HANG" countdown.
                mTimer = new CountDownTimer(COUNTDOWN_DURATION, 1000) {
                    public void onTick(long millisUntilFinished) {
                        mTextView.setText(String.valueOf(millisUntilFinished / 1000));
                        TextView statusTextView = getView().findViewById(R.id.statusTextView);
                        statusTextView.setText("행");
                    }

                    // Starts the "REST" countdown.
                    public void onFinish() {
                        long restTime = REST_DURATION / 1000;
                        long minutes = restTime / 60;
                        long seconds = restTime % 60;
                        mTextView.setText(String.format("%02d:%02d", minutes, seconds));
                        TextView statusTextView = getView().findViewById(R.id.statusTextView);
                        statusTextView.setText("잠시 쉬다");
                        mTimer = new CountDownTimer(REST_DURATION, 1000) {
                            public void onTick(long millisUntilFinished) {
                                long restTime = millisUntilFinished / 1000;
                                long minutes = restTime / 60;
                                long seconds = restTime % 60;
                                mTextView.setText(String.format("%02d:%02d", minutes, seconds));
                            }

                            public void onFinish() {
                                if (mCurrentSet < NUM_SETS) {
                                    mCurrentSet++;
                                    TextView setTextView = getView().findViewById(R.id.setTextView);
                                    setTextView.setText("세트 " + mCurrentSet);
                                    startTimer();
                                } else {
                                    mIsTimerRunning = false;
                                    mButton.setText(R.string.start);
                                    mCurrentSet = 1;
                                }
                            }
                        }.start();
                    }
                }.start();
            }
        }.start();
    }

    private void stopTimer() {
        mIsTimerRunning = false;
        mButton.setText(R.string.start);
        mTextView.setText("10");
        mCurrentSet = 1;
        TextView setTextView = getView().findViewById(R.id.setTextView);
        setTextView.setText("세트 1");
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        TextView statusTextView = getView().findViewById(R.id.statusTextView);
        statusTextView.setText("준비가 된?");
    }
}
Matt
  • 1

0 Answers0