当 phone 进入睡眠模式时关闭 AlarmManager

Turn off AlarmManager when phone goes to sleep mode

我将 AlarmManager 设置为每分钟触发一次。但是,我只在 phone 处于活动状态时才需要它。我想在 phone 进入睡眠状态时关闭所有更新。我不确定当 phone 进入睡眠状态时 AlarmManager 和小部件的默认行为通常是什么。但我注意到我的电池在睡眠模式下会下降。因此,我相信是小部件导致了它的失败。

那么,如果 AlarmManager 和其他广播在默认情况下继续触发,我如何才能在睡眠模式下关闭这些更新,然后在 phone 恢复活动模式时重新打开它们?

使用 public void setInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) 并将 RTC 作为类型。只有 RTC_WAKEUP 和 ELAPSED_REALTIME_WAKEUP 会唤醒设备。

例如在我的代码中我使用

am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + syncInterval, syncInterval, pendingIntent);