从setRepeating()的文档:
As of API 19, all repeating alarms are inexact.
而且,setRepeating()不适用于Doze.
您应该使用精确警报(根据设备的API级别通过适当的AlarmManager方法设置):
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
triggerTime, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
}
每当他们开火时重新安排他们.
对于重新安排,您可以将原始触发时间添加到Intent:
intent.putExtra(KEY_TRIGGER_TIME, triggerTime);
然后在onReceive()中检索此额外内容,将所需的间隔添加到它并使用新值重新安排警报:
@Override
public void onReceive(Context context, Intent intent) {
long triggerTime = intent
.getLongExtra(KEY_TRIGGER_TIME, System.currentTimeMillis());
// adding one day to the current trigger time
triggerTime += TimeUnit.DAYS.toMillis(1);
// set a new alarm using the new trigger time
// ...
}
注意:正如上面评论中提到的@Opiatefuchs,一些制造商(如小米或华为)可能会实施某些节电功能,可以防止警报被触发,并且不能以编程方式绕过.