闹钟未唤醒 phone

Alarm not waking phone

我希望我的应用程序经常唤醒。我用来设置闹钟的代码是:

AlarmManager alarmManager = (AlarmManager) getSystemService( Context.ALARM_SERVICE );
alarmManager.setExact( AlarmManager.RTC_WAKEUP, System.currentTimeMillis( ) + pollInterval, pendingIntent );

报警处理程序中有代码可以用上面的代码重置报警。

问题是当phone处于深度睡眠时(例如,在凌晨),闹钟不响。

有什么建议吗?

如果您使用 phone 和 Android 6.0 或更高版本,则必须处理 Doze 机制。因此,当 phone 处于休眠状态时,AlarmManager 不会立即工作。文档说:

Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.

If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().

所以,只需尝试对 API > 22

使用 setExactAndAllowWhileIdle()

文档here