Android 警报管理器 setExactAndAllowWhileIdle() 在处于打盹模式的 Android 7.0 Nougat 中不起作用 [英] Android Alarm Manager setExactAndAllowWhileIdle() not working in Android 7.0 Nougat in Doze mode

查看:101
本文介绍了Android 警报管理器 setExactAndAllowWhileIdle() 在处于打盹模式的 Android 7.0 Nougat 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用警报管理器的 setExactAndAllowWhileIdle 每 30 分钟在我的应用程序中触发一次警报,但它不起作用!

I am trying to make an alarm fire in my app every 30 minutes using Alarm Manager's setExactAndAllowWhileIdle but it is not working!

我通过在收到警报信号时发出推送通知来测试功能.

I test the functionality by issuing a push notification whenever I receive an alarm signal.

问题是:当设备闲置一段时间后进入打盹模式时,我不再收到警报.但是,一旦打开屏幕,我就会收到通知.我的应用程序需要准确的警报,需要每 30 分钟准时发送一次!由于设备处于打盹模式,无法接收延迟警报或掉线警报!

The problem is: when the device enters doze mode after being idle for sometime, I no longer receive alarms. However, as soon as I turn On the screen, I receive a notification. My app needs accurate alarms that need to be delivered exactly on-time every 30 minutes! It can not afford to receive delayed alarms or dropped ones because the device is in Doze mode!

我在代码中使用了以下内容:

  1. 我在打开应用时设置了闹钟.
  2. 我使用 WakefulBroadcastReceiver 接收警报信号.在其onReceive() 方法我设置了下一个闹钟.我也,开始一个startWakefulService 只发出推送通知,然后停止
  3. 我在 onReceive() 结束时调用 completeWakefulIntent.
  4. 我尝试同时测试:RTC_WAKEUP &ELAPSED_REALTIME_WAKEUP
  1. I set the alarm when I open my app.
  2. I receive the alarm signal using a WakefulBroadcastReceiver. In its onReceive() method I set the next alarm. I also, start a startWakefulService that only issues a push notification, then stops itself.
  3. I call completeWakefulIntent at the end of the onReceive().
  4. I tried testing both: RTC_WAKEUP & ELAPSED_REALTIME_WAKEUP

注意事项:

  • wakefulbroadcastReceiver 类已在清单中注册.
  • 我添加了以下权限:android.permission.WAKE_LOCK
  • 我尝试将我的应用列入白名单,但结果相同
  • 我尝试使用 setAlarmClock() 即使在打瞌睡期间也能正常工作模式,每 50 个警报有一个下降/延迟警报.所以,它也不是完美的.而且我不希望用户一直看到闹钟图标
  • setExactAndAllowWhileIdle() 不仅在打瞌睡期间不起作用,而且它它在工作时具有可怕的准确性.我通常会收到很多警报信号
    1-3 分钟后或 1-3 分钟前.
  • 我正在使用华为 Mate 8 和 android 7.0 Nougat 进行测试.
  • The wakefulbroadcastReceiver class is registered in the Manifest.
  • I added permissions for : android.permission.WAKE_LOCK
  • I tried White-Listing my app, but the results are the same
  • I tried using setAlarmClock() which worked all the time even during doze mode, with one dropped/delayed alarm every 50 alarms. So, it is also not perfect. And I don't want the user to see an alarm icon all the time up there.
  • Not only does setExactAndAllowWhileIdle() not work during doze, but also it has terrible accuracy when it is working. I usually get lots of alarm signals
    either 1-3 minutes later or 1-3 minutes earlier.
  • I am testing using Huawei Mate 8, and android 7.0 Nougat.

附注:在回答之前,请确保您了解启动 Android 6.0 M 和 Doze 模式的限制.

P.S: Before answering please make sure you are aware of the restrictions imposed starting Android 6.0 M and Doze mode.

链接1:https://developer.android.com/培训/监控设备状态/doze-standby.html

总而言之,它是这样说的:

In summary it says this:

  • 如果您需要设置在打瞌睡时触发的闹钟,请使用setAndAllowWhileIdle() 或 setExactAndAllowWhileIdle().
  • 使用 setAlarmClock() 设置的警报继续正常触发 —系统在这些警报触发前不久退出 Doze.

现在,为什么我不能使用 setExactAndAllowWhileIdle() 每 30 分钟获得一次准确的警报信号?!而且,为什么 setAlarmClock() 不是 100% 可靠的?!

Now, why don't I get accurate alarm signals every 30 minutes using setExactAndAllowWhileIdle()?! And, why isn't setAlarmClock() 100% reliable?!

推荐答案

你可能会得到准确的警报,但 处于打盹模式

You might get accurate alarms, but in Doze mode

系统忽略唤醒锁.

所以看起来好像 AlarmManager.setAlarmClock 是唯一可以接受的解决方案,如果您确实需要每 30 分钟触发一次.这可能会抵消所有打盹模式的节能...

So it seems as though AlarmManager.setAlarmClock is the only acceptable solution if you really need to trigger every 30 minutes. This will probably negate all doze mode energy savings...

顺便说一句:似乎您可以使用 adb shell dumpsys alarm 看到警报.

BTW: it seems like you can see alarms with adb shell dumpsys alarm.

可能性:使用 Firebase JobDispatcher

Firebase JobDispatcher 是一个用于在您的 Android 应用中安排后台作业的库.它提供了一个兼容 JobScheduler 的 API,适用于安装了 Google Play 服务的所有最新版本的 Android(API 级别 14+).

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 14+) that have Google Play services installed.

这篇关于Android 警报管理器 setExactAndAllowWhileIdle() 在处于打盹模式的 Android 7.0 Nougat 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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