Android如何每N小时调用一次方法O'Clock<<<<<<<<<<<

时间:2014-08-05 03:37:00

标签: android

我已经创建了一个服务,它将使用循环每小时调用Method,但不能完全是小时O'Clock(N:00), 我的代码

    try {
        do {
            Thread.sleep(Integer.parseInt(UtilitiesGlobals.ONE_HOUR));
            publishProgress();
        } while (RUNNING);
    } catch (Exception e) {
        e.printStackTrace();
    }

那么,我怎样才能打电话给每一小时O'Clock(1:00,2:00,3:00,4:00 ......)

2 个答案:

答案 0 :(得分:0)

Timer timer = new Timer ();
TimerTask hourlyTask = new TimerTask () {
    @Override
    public void run () {
        // your code here.
    }
};

// schedule the task to run starting now and then every hour.
timer.schedule (hourlyTask, 0l, 1000*60*60);

答案 1 :(得分:0)

final Handler handler = new Handler();
                    handler.postDelayed( new Runnable() {

                        @Override
                        public void run() {
                            todoItems.clear();

                            publishProgress();
                            handler.postDelayed( this, 60 * 60 );
                        }
                    }, 60 * 60 );*