Jamie Rico
Jamie Rico

Reputation: 27

How to use intervals in Android CountdownTimer

I am creating a countdown time in Android and I have the timer part working perfectly. However, I want to add a feature where the user can set the number of times they wish the timer to repeat. This is my current attempt at implementation

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

    textView = (TextView) findViewById(R.id.textView);
    minutesText = (EditText) findViewById(R.id.minutesText);
    secondsText = (EditText) findViewById(R.id.secondsText);
    startButton = (Button) findViewById(R.id.startButton);
    intervalCount = (EditText) findViewById(R.id.intervalCount);






    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (minutesText.getText().toString().equals("")) {
                minutesInt = 0;
            } else {
                minutesString = minutesText.getText().toString();
                minutesInt = Integer.parseInt(minutesString);

            }

            secondsString = secondsText.getText().toString();
            if (secondsText.getText().toString().equals("")) {
                secondsInt = 0;

            } else {
                secondsString = secondsText.getText().toString();
                secondsInt = Integer.parseInt(secondsString);

            }
            intervalsString = intervalCount.getText().toString();
            intervals = Integer.parseInt(intervalsString);


            final int timerAmount = ((minutesInt * 60) + (secondsInt)) * 1000;
            CountDownTimer timer = new CountDownTimer(timerAmount, 1000) {
                @RequiresApi(api = Build.VERSION_CODES.N)
                public void onTick(long millisUntilFinished) {
                    isRunning = true;
                    String timeLeft = String.format("%02d : %02d",
                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
                    );
                    textView.setText("Reamining: " + timeLeft);
                }
                public void onFinish() {
                    isRunning = false;
                    intervals--;
                    if (intervals > 0) {
                        timer.start();
                    }


                    try {
                        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                        r.play();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };timer.start();

        }
    });

My error is in the onFinish() it wont let me use timer.start()

Upvotes: 0

Views: 64

Answers (1)

Debashis Choudhury
Debashis Choudhury

Reputation: 3392

CountDownTimer basically uses a Handler thread which is like other threads can not be restarted once its ended.

You need to use a new instance of the CountDownTimer in each interval.

So in your case, bring the

intervals--;
if (intervals > 0) {

    // for each interval left create a new CountDownTimer here and start it.
}

outside of

new CountDownTimer(timerAmount, 1000 } {


    // do your operations here.

}

Upvotes: 1

Related Questions