Android倒计时计时器每5分钟运行一次

eimct9ow  于 2023-03-16  发布在  Android
关注(0)|答案(1)|浏览(234)

我想每5分钟运行一次的Android倒计时计时器,如果当前时间是02:33,那么计时器应在02:35自动运行5分钟,然后停止,并在02:40再次启动。
这里我想要传递Int T =下一个第5分钟,如果当前最小值是31,那么我想要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();
    }
0md85ypi

0md85ypi1#

请尝试使用此代码:

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;
 }

}

相关问题