运行 日期数组上的 CountDownTimer

Run CountDownTimer on array of dates

我的对象数组如下所示:

public class Time {

    public String start_time;

    public String finish_time;

    public Time(String start_time, String finish_time) {
        this.start_time = start_time;
        this.finish_time = finish_time;
    }
}

我需要按以下方式在我的 Fragment 中实现一个计时器:

它应该从数组中的第一个元素开始倒计时,在单个 Time 元素上它应该首先开始倒计时到到达 start_time 的剩余时间,然后当计时器到达 start_time,它应该开始倒计时到 finish_time,最终,当它到达 finish_time 时,它应该对数组中的下一个元素执行相同的先前操作。当整个数组完成时,它应该显示 00:00:00.

PS:start_time 和 finish_time 的格式如下:HH:mm 但是计时器应该是 HH:mm:ss

任何人都可以帮助实施或至少提供一个想法吗?

终于找到合适的答案了。非常感谢帮助我的人:

class克拉兹{

private Timer dateTimer;

private Timer remainderTimer;

private Date nextDate;

private boolean remainderTimerStarted;

private static final long REMINDER_UPDATE_INTERVAL = 1000;

private static final String[] DATES = { "12.04.2015 22:21", "12.04.2015 22:22", "12.04.2015 22:23" };

private int currentIndex;

public Clazz() {
    dateTimer = new Timer();
}

public static void main(String[] args) {
    Clazz instance = new Clazz();
    instance.run();
}

private void run() {
    nextDate = parseDate(DATES[currentIndex]);
    schedule();
}

public void schedule() {
    runSecondsCounter();
    dateTimer.schedule(new TimerTask() {

        @Override
        public void run() {

            System.out.println("Current date is:" + new Date());
            currentIndex++;
            if (currentIndex < DATES.length) {
                nextDate = parseDate(DATES[currentIndex]);
                System.out.println("Next date is:" + nextDate);
                schedule();
            } else {
                remainderTimer.cancel();
            }
        }
    }, nextDate);

}

private Date parseDate(String nextDate) {
    Date date = null;
    DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm",
            Locale.ENGLISH);
    try {
        date = format.parse(nextDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}

private void runSecondsCounter() {
    if (remainderTimerStarted) {
        remainderTimer.cancel();
    }

    remainderTimer = new Timer();
    remainderTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            remainderTimerStarted = true;
            long remains = nextDate.getTime() - new Date().getTime();
            System.out.println("Remains: " + (remains / 1000) + " seconds");
        }
    }, REMINDER_UPDATE_INTERVAL, REMINDER_UPDATE_INTERVAL);
}

}