gpt4 book ai didi

Android AlarmManager.setExactAndAllowWhileIdle() 警报在工作一段时间后未触发

转载 作者:行者123 更新时间:2023-11-29 18:36:20 30 4
gpt4 key购买 nike

我正在使用 AlarmManager 设置 2 个不同的重复警报。每小时第 59 分钟触发一个警报。此警报始终正常工作。但是,每 5 分钟安排一次的另一个警报在 1 或 2 小时内开始正常工作。在那之后,我的 AlarmManager 会安排警报,但它再也不会触发了。

我正在使用 setExactAndAllowWhileIdle() 方法来安排警报。

为了检查警报是否已安排和激活,我在调用 setExactAndAllowWhileIdle() 后使用此方法:

fun isAlarmActive(frameType: FrameType): Boolean{
val alarmIntent = getAlarmIntent(frameType)
alarmIntent.action = CUSTOM_INTENT
return PendingIntent.getBroadcast(ctx, 0, alarmIntent, PendingIntent.FLAG_NO_CREATE) != null
}

我通过安排和注释掉执行 setExactAndAllowWhileIdle() 的行来测试此方法,然后将其输出与“adb shell dumpsys alarm”输出进行对比。

我让程序在 24 小时内正常工作。查看我的日志后,我看到警报在 2 小时内工作,当它停止工作时,它被激活但不再触发......

而且我的应用程序没有被杀死,因为尽管这个警报停止被触发,但另一个警报仍在继续工作,我遵循与下面的代码完全相同的机制。

这是 MyAlarmManager 类:

class MyAlarmManager(private val ctx: Context) {

private val TAG = "MyAlarmManager"
val CUSTOM_INTENT = "com.test.intent.action.ALARM"

private val mAlarmManager = ctx.getSystemService(Context.ALARM_SERVICE) as AlarmManager

fun setAlarm(frameType: FrameType, delayInMinutes: Int, force: Boolean) {
LogsHandler.writeLogs(TAG, "setAlarm()")
checkAlarmsToCancel(frameType)
val triggerAt = getTriggerTime(delayInMinutes, force)
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAt.timeInMillis, getPendingIntent(frameType))
LogsHandler.writeLogs(TAG, "Se ha programado la alarma de tipo $frameType a la hora: ${Date(triggerAt.timeInMillis)}")

if(isAlarmActive(frameType))
LogsHandler.writeLogs(TAG, "La alarma está activada")
else
LogsHandler.writeLogs(TAG, "La alarma NO está activada")
}
private fun getAlarmIntent(frameType: FrameType) :Intent{
return when(frameType){
FrameType.PRESENCE -> Intent(ctx, StartPresenceReportService::class.java)
FrameType.STEPS -> Intent(ctx, StartStepsCountReportService::class.java)
FrameType.LOCATION -> Intent(ctx, StartLocationDeliveryService::class.java)
}
}

private fun getPendingIntent(frameType: FrameType): PendingIntent {
val alarmIntent = getAlarmIntent(frameType)
alarmIntent.action = CUSTOM_INTENT
return PendingIntent.getBroadcast(ctx, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT)
}

}


这是我的广播接收器:

class StartLocationDeliveryService: BroadcastReceiver() {

private var TAG = "StartLocationDeliveryService"

override fun onReceive(context: Context, intent: Intent) {

//Irrelevant code
LocationDeliveryService().executeWork(context, intent)
}
}

这是 LocationDeliveryService:

class LocationDeliveryService {
private var TAG = "LocationDeliveryService"

fun executeWork(context: Context, intent: Intent) {

//Business logical operations
MyAlarmManager(context).setAlarm(FrameType.LOCATION, minutesBetweenLocationDeliveryMsgs, false)
}
}

最佳答案

我自己解决了这个问题。这不是编程问题,而是与 Android 及其 Doze 模式有关。 Wear OS 有一个限制,即我们只能每 9 分钟安排一次闹钟。

所以我启动了一个测试,每 10 分钟重复一次我的警报,然后问题就消失了!希望我的解决方案可以帮助其他人!

关于Android AlarmManager.setExactAndAllowWhileIdle() 警报在工作一段时间后未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54179554/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com