我已将 ScheduledExecutorService 设置为每 5 秒打印一次:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("working");
}
}, 5, 5, TimeUnit.SECONDS);
一切都很完美,但是在活动进入 onPause() 加上几分钟后,它开始以随机间隔打印。
正常工作的例子:
10-17 11:05:04.865: I/System.out(3863): working:
10-17 11:05:09.865: I/System.out(3863): working:
10-17 11:05:14.865: I/System.out(3863): working:
10-17 11:05:19.865: I/System.out(3863): working:
10-17 11:05:24.865: I/System.out(3863): working:
10-17 11:05:29.865: I/System.out(3863): working:
10-17 11:05:34.865: I/System.out(3863): working:
10-17 11:05:39.865: I/System.out(3863): working:
10-17 11:05:44.865: I/System.out(3863): working:
10-17 11:05:49.855: I/System.out(3863): working:
10-17 11:05:54.855: I/System.out(3863): working:
10-17 11:05:59.865: I/System.out(3863): working:
在 onPause() 活动 + 几分钟后它是如何工作的示例:
10-17 11:24:21.825: I/System.out(3959): Fragment onPause
10-17 11:24:35.484: I/System.out(3959): working
10-17 11:24:43.533: I/System.out(3959): working
10-17 11:24:52.084: I/System.out(3959): working
10-17 11:25:15.490: I/System.out(3959): working
10-17 11:25:34.908: I/System.out(3959): working
10-17 11:25:59.146: I/System.out(3959): working
10-17 11:26:18.385: I/System.out(3959): working
10-17 11:26:45.162: I/System.out(3959): working
10-17 11:27:24.991: I/System.out(3959): working
10-17 11:27:55.735: I/System.out(3959): working
10-17 11:28:35.301: I/System.out(3959): working
10-17 11:29:12.415: I/System.out(3959): working
这东西正常吗?是 scheduleAtFixedRate 的替代品吗?(我不想使用服务,因为在普通应用程序中我将以 24 小时的速率安排任务,并且我不希望在应用程序未运行时触发此事件,所以 AlarmManager 不是那个我也要)。