0

I want android countdown timer that run every 5th mins, If current time is 02:33 then timer should run automatically at 02:35 for 5 mins and then stop and start again at 02:40.

here i want to pass Int T = next 5th min, if current min is 31 then i want 35.

private void Timer(int t){
        Log.i("snake", String.valueOf(t));
        new CountDownTimer(t*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long second = (millisUntilFinished / 1000) % 60;
                long minutes = (millisUntilFinished/(1000*60)) % 60;
                timer.setText("Time Left - " + minutes + ":" + second);

            }

            @Override
            public void onFinish() {
               
                timer.setText("Finish");
               // Timer();
            }
        }.start();
    }
  • you have to set before an alarm service (https://stackoverflow.com/questions/24822506/how-to-start-a-service-at-a-specific-time) and when service is invoked you can start your CountDownTimer. With code you posted, the CountDownTimer starts in invocation-time. – Paolino L Angeletti Mar 15 '23 at 09:36
  • how to get next 5th min? – Ravi Dixit Mar 15 '23 at 09:37

1 Answers1

0

try with this code:

try with this code:

import java.time.LocalDateTime;

public class HelloWorld{

 public static void main(String []args){
    final LocalDateTime now = LocalDateTime.now();
    System.out.println("Current time: " + now);
    final int minute = now.getMinute();
    System.out.println("Current minute: " + minute);
    final int time_to_add = get_time_to_add(minute);
    System.out.println("Time to add: " + time_to_add);
    final int next_5_minute = (minute + time_to_add) < 60 ? (minute + time_to_add) : 0;
    System.out.println("Next 5 minute: " + next_5_minute);
 }
 
 private static int get_time_to_add(final int currMinutes)
 {
     int toReturn = 5;
     int newMinute = currMinutes + toReturn;
     while(newMinute % 5 != 0){
         toReturn -= 1;
         newMinute = currMinutes + toReturn;
     }
     return toReturn;
 }

}