0
new CountDownTimer(1000000, 1000) {
            public void onTick(long millisUntilFinished) {
                // Used for formatting digit to be in 2 digits only
                NumberFormat f = new DecimalFormat("00");
                long min = (millisUntilFinished / 60000) % 60;
                long sec = (millisUntilFinished / 1000) % 60;
                timer.setText("OTP Expires in: " + f.format(min) + ":" + f.format(sec));
            }

I am supposed to delete a OTP after the timer runs out which in this case is 10 minutes. I am not sure how to set the timer to 10 minutes.

Tech Geek
  • 11
  • 1
  • 10 minutes is 600000 milliseconds – Lal Jul 14 '22 at 12:15
  • check this answer: https://stackoverflow.com/a/36713370/17286199 it contains an example and also shows how to stop the Countdown and prevent memory leak. You'll want to trigger OTP deletion in onFinish() method – AndroIdefix Jul 14 '22 at 12:31

2 Answers2

1

You can see it in CountDownTimer

 new CountDownTimer(1000000, 1000) {

     public void onTick(long millisUntilFinished) {
                NumberFormat f = new DecimalFormat("00");
                long min = (millisUntilFinished / 60000) % 60;
                long sec = (millisUntilFinished / 1000) % 60;
     }

     public void onFinish() {
         timer.setText("OTP Expires in: " + f.format(min) + ":" + f.format(sec));
     }
 }.start();

 
Corzel
  • 21
  • 6
0

try this

    new CountDownTimer(60*10*1000, 1000) {

        public void onTick(long millisUntilFinished)
        {
            binding.time.setText("seconds remaining: "
                    + millisUntilFinished / 1000);

        }

        public void onFinish() {

        }
    }.start();
Praveen Kumar
  • 329
  • 2
  • 14