且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android完全闹钟始终关闭3分钟

更新时间:2023-02-12 10:07:17

问题似乎仅发生在三星设备上(例如Galaxy Grand,S4,S5,S6,Note 3,Note 4和Lollipop(5.0、5.1、5.1.1)。当设备由电池供电且屏幕关闭时,警报调度的时间似乎不准确。如果设备正在充电或在计划警报期间打开屏幕,则不会发生此问题。

The issue seems to occur only on Samsung devices (e.g. Galaxy Grand, S4, S5, S6, Note 3, Note 4) with Lollipop (5.0, 5.1, 5.1.1). It seems that alarms are scheduled inexact when device is on battery with screen off. If device is charging or has screen on during scheduling alarm the issue will not occur.

您可以通过以下方式验证下一个警报是否不准确:

You can verify if next alarm will be inexact with:

adb shell dumpsys alarm

我没有找到解决此问题的完美解决方案-只是每个都有一些缺点的解决方法:

I didn't find perfect solution for this problem - only workarounds where each has some drawbacks:


  1. 使用 setAlarmClock 代替 setExact (请参阅此答案)。此方法效果很好(并非在所有设备上都适用),但是此解决方案的问题在于,警报将通过在状态栏中显示警报图标(如果某人尚未设置闹钟)并显示下一个警报预定时间来影响系统不幸的是,虽然这在5.1.1的Galaxy Grand上可以使用,但在5.0.1的Galaxy S4上却不可用。

  2. 在安排警报之前启用屏幕(我做了一半在安排下一个警报之前,避免发生竞争情况。当然,并不是每个应用程序都启用屏幕只是为了调度下一个警报是一个好的解决方案。

  3. 一个描述类似问题的错误报告将其与应用程序包名称的长度连接起来!我没有验证它是否真的可以解决问题,因为更改软件包名称不是已发布应用程序的选项。

  4. 另一份报告,其中有人声称可以使用 WakefulBroadcastReceiver 来解决此问题。 ,但在我的情况下不起作用。

  1. Use setAlarmClock instead setExact (see this answer). This works very well (not on all devices), but the issue with this solution is that the alarm will affect the system by showing alarm icon in status bar (if someone doesn't have alarm clock set already) and displaying next alarm scheduled time on alarm widgets etc. Unfortunately while this works on Galaxy Grand with 5.1.1 it doesn't on Galaxy S4 with 5.0.1.
  2. Enable screen before scheduling the alarm (I do this half second before scheduling next alarm to avoid race condition). Of course it is not good solution for every app to enable screen just to schedule next alarm.
  3. One bug report describing similar issue connects it with app package name length! I didn't verify if it really fixes the issue, because changing package name is not an option for already published app.
  4. There is another report where someone claims this can be fixed by using WakefulBroadcastReceiver, but it doesn't work in my case.

BTW这个问题使我发疯:)

BTW This issue drives me crazy :)

编辑:当应用包名称中有关键字 alarm或 alert时,看来不会发生此问题(如Mathieu H.在下面的评论中所指出的那样) )。

Looks like this issue does not occur when there is keyword "alarm" or "alert" in the app package name (as pointed out by Mathieu H. in comments below).

我还可以通过在电池设置(或Smart Manager应用)中禁用应用优化来手动解决此问题。似乎无法通过编程方式完成,因此您可以尝试询问用户...

I was also able to fix the issue manually by disabling App optimization in Battery settings (or in Smart Manager app). It seems it cannot be done programmatically, so you can try asking your users...