How to Countdown to a day using Android CountDownTimer

后端 未结 3 863
野的像风
野的像风 2021-02-06 19:39

I\'m new here and new to Android Development, I Have a question regarding the Android CountDownTimer. How am I able to use Android\'s CountDownTimer, to count down to a specific

相关标签:
3条回答
  • 2021-02-06 20:08
    Date currentDate=new Date();
    Date endDate=new Date();
    long difference = endDate.getTime() - currentDate.getTime();
    new CountDownTimer(difference, 1000)     {
    
    public void onTick(long millisUntilFinished) {
    
    }
    
    public void onFinish() {
      System.out.println("Done");
     }
    }.start();
    
    0 讨论(0)
  • 2021-02-06 20:16

    Helpful method !

    @Override
    public void start(final long miliSecond, final int interval) {
    
        new CountDownTimer(miliSecond, interval) {
            @Override
            public void onTick(long millisUntilFinished) {
                long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                millisUntilFinished -= TimeUnit.DAYS.toMillis(day);
    
                long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
                millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);
    
                long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);
    
                long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
    
                intDay.setText("" + day);
                intHour.setText("" + hour);
                intMinute.setText("" + minute);
                intSecond.setText("" + second);
            }
    
            @Override
            public void onFinish() {
                // What ever you want !
            }
    
        }.start();
    
    }
    
    0 讨论(0)
  • 2021-02-06 20:18

    This is exactly how you do it

    UPDATE 3

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.your_layout);
    
        TextView tv_countdown = (TextView) findViewById(R.id.your_textview_id);
    
        Calendar start_calendar = Calendar.getInstance();
        Calendar end_calendar = Calendar.getInstance();
    
        long start_millis = start_calendar.getTimeInMillis(); //get the start time in milliseconds
        long end_millis = end_calendar.getTimeInMillis(); //get the end time in milliseconds
        long total_millis = (end_millis - start_millis); //total time in milliseconds
        end_calendar.set(2015, 10, 6); // 10 = November, month start at 0 = January
    
        //1000 = 1 second interval
        CountDownTimer cdt = new CountDownTimer(total_millis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
                millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
    
                long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
                millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
    
                long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
    
                long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
    
                tv_countdown.setText(days + ":" + hours + ":" + minutes + ":" + seconds); //You can compute the millisUntilFinished on hours/minutes/seconds
            }
    
            @Override
            public void onFinish() {
                tv_countdown.setText("Finish!");
            }
        };
        cdt.start();
    }
    
    0 讨论(0)
提交回复
热议问题