1

我正在使用 mhandler.postDelayed(runnable,5000) 每 5 秒将数据写入文件。

当屏幕关闭时,我从文件中观察到 postDelayed(runnable,5000) 的时间。它在一段时间内是准确的,但在一小时后,postDelayed(runnable,5000) 的时间增加到 10 秒。

两小时后,postDelayed(runnable,5000) 的时间增加到 15 秒。三小时后,postDelayed(runnable,5000) 的时间增加到 20 秒,依此类推。

postDelayed 触发时机随时间变化,这似乎很奇怪。

我期待这个还是我的代码中有错误?

4

1 回答 1

0

您应该尝试使用除处理程序之外的其他东西。

试试计时器

int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        //...Perform a task...
    }
};
new Timer(delay, taskPerformer).start();
于 2013-06-21T08:16:23.250 回答