【问题标题】:Alarm Manager stops trigger PendingIntent after few moments几分钟后,警报管理器停止触发 PendingIntent
【发布时间】:2023-12-04 20:48:01
【问题描述】:

我尝试创建提醒应用程序。我的目标是每小时触发一次通知。我已经使用警报管理器来实现它。前 2-3 小时一切正常。在那之后,通知将停止发送。如果我重新打开应用程序,我会立即收到丢失通知。

报警管理器:

val intent = Intent(context, AlarmReceiver::class.java).apply {
        action = context.getString(R.string.alarm_pending_action)
    }
val alarmIntent =  PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT)
val datetimeToAlarm = Calendar.getInstance()
        .apply {
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MILLISECOND, 0)
        }
alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        datetimeToAlarm.timeInMillis,
        60 * 60 * 1000,
        alarmIntent
)

报警接收器:

override fun onReceive(context: Context, intent: Intent) {
    if (intent.action != null) {
        if (intent.action!!.equals(context.getString(R.string.alarm_pending_action), ignoreCase = true)) {
            NotificationHelper.createNotification(context)
        }
    }
}

AndroidManifest

<receiver android:name=".alert.AlarmReceiver"/>

感谢您的帮助

【问题讨论】:

  • 我尝试使用 Service/IntentService 而不是 BroadcastReceiver。但一切都没有改变。

标签: alarmmanager


【解决方案1】:

我解决了我的问题。解决方案是将setRepeating替换为setAlarmClock,效果很好。

【讨论】: