setAlarmClock()在打ze模式下触发得太晚 [英] setAlarmClock() fires too late in doze mode

查看:533
本文介绍了setAlarmClock()在打ze模式下触发得太晚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难使无线电闹钟按预期工作,并且我在这里阅读了很多有关该主题的主题,但不幸的是没有一个帮助我。

I have soooo much trouble getting my radio alarm clock working as intended and I have read a lot of threads here about that topic, but unfortunatley none did help me.

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);        
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent penInt = PendingIntent.getBroadcast(this, intentId, intent, 0);

这种在我在stackoverflow上找到的API级别之间的区分方法,将其放入我的 calcNextAlarm()函数(加上一些调试日志消息),无论设备使用什么API,都可以正确设置警报:

This method of distinction between the API levels I found here on stackoverflow and put it inside my calcNextAlarm() function (plus some log messages for debugging) to set the alarm correctly no matter what API is being used on the device:

// problems in doze mode api 23+
if (Build.VERSION.SDK_INT >= 23) {
    if (testMode) Log.d("Ben", "setAlarmClock() - API 23+");
    am.setAlarmClock(new AlarmManager.AlarmClockInfo(alarmTimeInMillis, penInt), penInt);
}

else if (Build.VERSION.SDK_INT >= 19) {
// Wakes up the device in Idle Mode
    if (testMode) Log.d("Ben", "setExact() - API >= 19 && API < 23");
    am.setExact(AlarmManager.RTC_WAKEUP, alarmTimeInMillis, penInt);
}
// Old APIs
else {
    if (testMode) Log.d("Ben", "set() - API < 19");
    am.set(AlarmManager.RTC_WAKEUP, alarmTimeInMillis, penInt);
}

根据Log.d消息,我可以在Android 7.1上看到这一点设备正在执行第一个方法 setAlarmClock()以在接收器中设置警报。

According to the Log.d messages I can see that, on my Android 7.1 Device the first method setAlarmClock() is being executed to set the alarm in the Receiver.

我真的经过3周的失败测试和编码后,我感到绝望-根据打ze模式培训页面,我的警报今天又响了4分钟-永远不会发生:

I am getting really desperate after 3 weeks of unsuccessful tests and coding - my alarm today got off 4 minutes too late again - this should never happen, according to the doze mode training page:


使用setAlarmClock()设置的警报继续正常触发-在这些警报触发之前,系统立即退出Doze。

Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.

在我的7.1手机上,当我将闹钟设置为现在+5或6分钟时,闹钟甚至会延迟20秒到1:40分钟。有人可以告诉我如何真正地始终及时地发出警报吗?

On my 7.1 phone the alarm will even be 20 seconds to 1:40 mins late when I set the alarm to "now +5 or 6" minutes. Could anyone advise me how to really ALWAYS get the alarm off perfectly in time?

推荐答案

尝试使用:

setExactAndAllowWhileIdle()

这将确保警报按时触发。我已经在自己的应用程序中对其进行了测试,并且它是可靠的。

This will ensure that the alarm fires on time. I have tested it in my own app and it's reliable.

如果目标API小于23,请将其保留在if子句中,该子句可检查安装在设备。仅在API级别23上使用此功能,其余的请继续使用setExact

If you are targeting API less than 23, keep this inside an if clause which checks the current api installed in the device. Use this only above API level 23, for rest keep using setExact

有权回答您的评论:

1)我已经在打ze模式下准确地测试了它,而没有插入电池。

1) i have accurately tested it in doze mode without the battery being plugged in

2)是的,很遗憾,闲置型警报器每9分钟限制一个警报。您有两种选择:

2) yea unfortunately there's the one alarm per 9 minutes limit for while idle type alarms. You have two alternatives here:

首先使用setExactAndAllowWhileIdle触发第一个警报。

First use the setExactAndAllowWhileIdle to fire the first alarm.

a)打sn ,您必须使用setAlarmClock方法

a) For snooze, you will have to use setAlarmClock method

b)暂停,您可以安排JobScheduler作业,并将最小延迟时间设置为暂停时间。这将确保至少在暂停间隔的间隔内安排作业。但这会导致在间隔后随机安排作业,因此您还需要将替代截止时间设置为0,以便在最小延迟后立即安排作业。还应将网络要求设置为无,将空闲模式设置为默认或false。根据我的经验,这也和警报的确切方法一样可靠,我本人也使用这种方法。

b) for snooze, you can have a JobScheduler job scheduled with minimum latency time set as your snooze time. This will ensure that the job gets at least scheduled at the gap of the snooze interval. But just this will cause the job to get randomly scheduled after the interval so you'll also need to have an override deadline set to 0 so that the job gets scheduled immediately after the minimum latency. Also keep network requirements as none and idle mode needed as default or false. This is also as reliable as the alarm exact methods as per my experience and i personally use this approach.

这篇关于setAlarmClock()在打ze模式下触发得太晚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆