Android计时器运行太频繁

时间:2015-08-06 17:50:10

标签: java android timer

我的代码中有一个计时器任务, checkHour 。此任务检查当前小时是否在特定时间之间,并且根据时间和更新是否打开,打开/关闭位置更新。

以下是我的计时器任务:

TimerTask checkHour = new TimerTask() {
    @Override
    public void run() {
        handler.post(new Runnable() {
            @SuppressWarnings("unchecked")
            public void run() {
                try {
                    Log.i("TIMER :: ", "CHECKING HOUR...");
                    LocationServices.getCurrentHour();
                    //If updates are off and it is before 7:00 am or after 5:00 pm, stop updates
                    if (LocationServices.currentHour >= endTime && updatesOn) {
                        Log.i("TURNING UPDATES :: ", "OFF");
                        LocationServices.stopLocationUpdates();
                    }

                    //If updates are off and it is between 7:00 am and 5:00 pm, start updates
                    if ((LocationServices.currentHour >= startTime && LocationServices.currentHour < endTime) && !updatesOn) {
                        Log.i("TURNING UPDATES :: ", "ON");
                        LocationServices.startUpdates(context, null);
                    }
                }
                catch (Exception e) {
                    // TODO Auto-generated catch block
                }
            }
        });
    }
};

我正在onCreate方法中实现它:

Timer timer = new Timer();
timer.schedule(checkHour, 1000, 600000); 

根据我在这里阅读的内容(http://developer.android.com/reference/java/util/Timer.html),计划应该在延迟后启动计时器任务(对我来说是1000毫秒)然后在每个周期后运行任务(600000毫秒)或者我10分钟)。但是,它的运行非常不一致。这是一个日志片段,用于检查小时的运行时间:

08-06 12:35:08.575  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:35:22.960  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:35:23.877  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:35:40.739  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:35:44.144  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:36:09.826  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:37:49.706  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:38:25.042  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:38:47.019  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:38:50.588  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:39:12.093  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:39:14.529  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...
08-06 12:39:20.179  27652-27652/org.apsugis.pickupdetect I/TIMER ::﹕ CHECKING HOUR...

我的问题是为什么我的计时器每隔一分钟左右而不是每10分钟运行一次?

0 个答案:

没有答案