1 回答
TA贡献2036条经验 获得超8个赞
尝试改变
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
到
long dateTime = calendar.getTimeInMillis();
if (dateTime <= System.currentTimeMillis()) {
time = dateTime + 24 * 3600 * 1000;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setWindow(AlarmManager.RTC_WAKEUP, dateTime ,
AlarmManager.INTERVAL_DAY, pendingIntent);
} else {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, dateTime ,
AlarmManager.INTERVAL_DAY, pendingIntent);
}
如果您阅读了与AlarmManager相关的 Android 文档
从 API 19 (Build.VERSION_CODES.KITKAT) 开始,警报传递不准确:操作系统将转移警报以尽量减少唤醒和电池使用。有新的 API 来支持需要严格交付保证的应用程序;请参阅setWindow(int, long, long, android.app.PendingIntent)和setExact(int, long, android.app.PendingIntent)。targetSdkVersion 早于 API 19 的应用程序将继续看到以前的行为,即所有警报都在请求时准确传递。
编辑:
我建议在完成各自的任务后,在您的 broadcastReceiver 中调用您的setAlarmDaily和方法。setAlarmRelease并更改一次警报的方法。像这样:
long dateTime = calendar.getTimeInMillis();
if (dateTime <= System.currentTimeMillis()) {
time = dateTime + 24 * 3600 * 1000;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//alarmManager.setExact(AlarmManager.RTC_WAKEUP, dateTime, pendingIntent); // not suggested
// 15 mins of window to call the alarm and have battery optimization
long windowMillis = 15 * 60 * 1000L;
alarmManager.setWindow(AlarmManager.RTC_WAKEUP, dateTime ,
windowMillis, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime, pendingIntent);
}
添加回答
举报