Android警报管理器setExactAndAllowWhileIdle()在打ze模式下无法在Android 7.0牛轧糖中使用 [英] Android Alarm Manager setExactAndAllowWhileIdle() not working in Android 7.0 Nougat in Doze mode

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

问题描述

我正在尝试使用警报管理器的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.

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

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(),即使在打ze睡时也可以一直使用 模式,每50个警报中有一个下降/延迟警报.所以,这也不是 完美的.而且我不希望用户一直看到警报图标 在那里.
  • setExactAndAllowWhileIdle()不仅在打Not睡时不起作用,而且还可以 工作时精度很差.我通常会收到很多警报信号
    1-3分钟后或1-3分钟前.
  • 我正在使用Huawei Mate 8和android 7.0牛轧糖进行测试.
  • 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.

PS: 在回答之前,请确保您了解启动Android 6.0 M和打ze模式所施加的限制.

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/training/monitoring-device-state/doze-standby.html

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

In summary it says this:

  • 如果您需要设置在打ze睡时触发的警报,请使用 setAndAllowWhileIdle()或setExactAndAllowWhileIdle().
  • 使用setAlarmClock()设置的警报继续正常触发- 警报发出前不久,系统退出Doze.
  • If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
  • Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.

现在,为什么我不使用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

系统忽略唤醒锁.

The system ignores wake locks.

因此,好像 AlarmManager.setAlarmClock 是唯一可接受的解决方案.这可能会抵消所有打do模式的节能效果.

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,该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()在打ze模式下无法在Android 7.0牛轧糖中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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