Android服务计划TaskExecutor将被调用两次

时间:2014-02-28 16:33:26

标签: android service

我正在编写 Android 应用程序。有一个服务和scheduleTaskExecutor调用一种方法,每分钟减少小时和分钟。你知道为什么scheduleTaskExecutor每分钟执行两次吗?或者为什么if / else有时会被忽略?应该有一个开始延迟和延迟执行1分钟......谢谢!

/**
 * Set reduce time thread
 */

public void setRunTime() {
    // counting work
    scheduleTaskExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {

            reduceTime();

        }
    }, 1, 1, TimeUnit.MINUTES);
}

/**
 * Reduce time
 * 
 */
public void reduceTime() {

    if (this.getHour() != 0) {

        if (this.getMinute() == 0) {

            // set Minute
            this.setMinute(60);

            Log.i("test1", this.getHour() + " " + this.getMinute());

            // reduce hour
            this.setHour(getHour() - 1);

            // Reduce time
            this.setMinute(getMinute() - 1);

            Log.i("test2", this.getHour() + " " + this.getMinute());

        } else {

            // Reduce time
            this.setMinute(getMinute() - 1);

            Log.i("test3", this.getHour() + " " + this.getMinute());
        }

    } else if (this.getHour() == 0 && this.getMinute() != 0) {
        this.setMinute(getMinute() - 1);

    } else {
        this.scheduleTaskExecutor.shutdown();
        this.stopSelf(getStartId());
    }

1 个答案:

答案 0 :(得分:0)

以下是答案:

使用处理程序。 Timer和ExecuterService存在问题。

请看这个链接:

Android Timer is called twice