【发布时间】:2016-06-29 08:39:44
【问题描述】:
我需要每 10 分钟计划一次计划任务。
由于在 Lollipop 和更高版本中 setRepeating() 不准确,我使用 setExact() 并且(在触发警报时)我在 10 分钟内设置了新的准确警报。
private void setAlarm(long triggerTime, PendingIntent pendingIntent) {
int ALARM_TYPE = AlarmManager.ELAPSED_REALTIME_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(ALARM_TYPE, triggerTime, pendingIntent);
} else {
alarmManager.set(ALARM_TYPE, triggerTime, pendingIntent);
}
}
triggerTime 计算为SystemClock.elapsedRealtime() + 600_000;
当警报响起时,我首先计划一个新的,然后才运行我计划的任务。
setAlarm();
mySheduledTask;
我的清单中有WAKE_LOCK 权限。
当我在 Android 4 上测试它时 - 它运行良好(偏差可能是 12-15 毫秒)。
但是当我在小米红米 Note 3 Pro (5.1.1) 上运行应用程序时 - 偏差可能高达 15 秒!
例如,我在日志文件中看到:第一次运行是在 1467119934477(RTC 时间),第二次 - 在 1467120541683。差异是 607_206 毫秒,而不是 600_000 ,正如它所计划的那样!
我错过了什么?什么是模拟系统警报行为的方法(这是可以描述我的策略的最接近的用例)?
PS.我为PendingIntent = PendingIntent.getService(context, 0, myIntent, 0);使用IntentService
【问题讨论】:
标签: android alarmmanager