AlarmManager 不会唤醒 phone
AlarmManager wouldn't wake up the phone
有一个代码应该使用 AlarmManager
在指定的精确时间触发操作(第二天在 7:00am):
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, FooIntentService::class.java)
val pendingIntent = PendingIntent.getService(this, 0, intent, 0)
// Set alarm
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 7)
calendar.set(Calendar.MINUTE, 0)
// Set tomorrow
calendar.add(Calendar.DATE, 1)
manager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
我已经测试过这段代码在最多 5 分钟内触发事件,关闭应用程序(关闭所有应用程序)并使其进入睡眠状态(按下保持按钮)- 它有效。然而,当我将明天的时间设置为 7:00am(距离现在还有 5 分钟以上)时 - 它永远不会触发,直到我解锁它(手动唤醒)。此刻我把它唤醒了——动作马上就触发了。
问题:我提供的示例代码是否正确设置我的计划事件?
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
基本上,警报的目的并不完全是为了尽量减少唤醒和电池使用。您可以将 set
替换为 setExact
然后它应该可以按您的要求工作。
manager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent);
有一个代码应该使用 AlarmManager
在指定的精确时间触发操作(第二天在 7:00am):
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, FooIntentService::class.java)
val pendingIntent = PendingIntent.getService(this, 0, intent, 0)
// Set alarm
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 7)
calendar.set(Calendar.MINUTE, 0)
// Set tomorrow
calendar.add(Calendar.DATE, 1)
manager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
我已经测试过这段代码在最多 5 分钟内触发事件,关闭应用程序(关闭所有应用程序)并使其进入睡眠状态(按下保持按钮)- 它有效。然而,当我将明天的时间设置为 7:00am(距离现在还有 5 分钟以上)时 - 它永远不会触发,直到我解锁它(手动唤醒)。此刻我把它唤醒了——动作马上就触发了。
问题:我提供的示例代码是否正确设置我的计划事件?
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
基本上,警报的目的并不完全是为了尽量减少唤醒和电池使用。您可以将 set
替换为 setExact
然后它应该可以按您的要求工作。
manager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent);