警报管理器的setExactAndAllowWhileIdle()无法正常工作 [英] setExactAndAllowWhileIdle() for alarmmanager is not working properly

查看:2042
本文介绍了警报管理器的setExactAndAllowWhileIdle()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在用户设置的确切时间执行特定操作的应用程序。为此,我使用 setExactAndAllowWhileIdle()方法,因为此文档说,具有android 6.0或更高版本的android设备具有休眠模式概念,即如果设备闲置一段时间,它将进入休眠模式,并且休眠模式会限制警报。如果我想在设备进入打ze模式时触发警报,则我有 setExactAndAllowWhileIdle()方法,如文档所述。本文档还包含手动方式,使设备进入打ze模式以进行测试。因此,我正在使用这种方式进行测试,但是当设备进入打ze模式时,并且当我通过终端命令停止打ze模式时,我的警报不会触发,过去的警报会立即触发。

I am developing an app which needs to perform particular action on the exact time which user has set. For this i am using setExactAndAllowWhileIdle() method because this documentation says that android devices having android 6.0 or above has doze mode concept in which if devices remains idle for some times then it will enter into doze mode and doze mode restricts alarms. If i want to fire my alarm when device is into doze mode then i have setExactAndAllowWhileIdle() method as documentation says. This documentation also contains manual way to enter device into doze mode for testing purpose. so, i am testing using that way but my alarm is not fired when device is into doze mode and when i stops doze mode via terminal command my past alarm will fires instantly.

所以,我的问题是 setExactAndAllowWhileIdle()此方法在打ze模式下不起作用,但必须如文档所述工作。我知道此方法的局限性,我每9分钟只能发出一个警报,并且我遵守此规则。因此,我无法理解问题出在哪里。

So, my problem is that setExactAndAllowWhileIdle() this method is not working in doze mode but it should have to work as said in documentation. I know limitation of this method that i can only fire one alarm per 9 minutes and i am following this rule. So, i can't understand where is the problem.

我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, d.getTime(), pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    alarmManager.setExact(AlarmManager.RTC, d.getTime(), pendingIntent);
else
    alarmManager.set(AlarmManager.RTC, d.getTime(), pendingIntent);

这是方法问题还是我做错方法了?

Is it a method problem or i am doing it in a wrong way??

推荐答案

我找到了解决我问题的方法,所以,我在这里发布了自己的答案,对我有用。

I've found the solution for my problem so, i am posting my own answer here which worked for me.

使用 setAlarmClock()方法解决了我的问题。如果您使用setAlarmClock()方法设置了警报,则将不允许系统在警报时间1小时之前进入打do模式。我通过设置警报后手动强制设备进入打ze模式进行了测试。让我解释一下完整的情况。

Using setAlarmClock() method has solved my problem. If you set alarm using setAlarmClock() method then this will not allow the system to go into doze mode before 1 hour of your alarm's time. I had tested this by manually forcing my device to go into doze mode after setting my alarm. Let me explain full scenario.


  1. 首先,我在当前时间5分钟后设置了闹钟,然后尝试将设备置于打ze模式




adb shell dumpsys deviceidle force-idle

adb shell dumpsys deviceidle force-idle

它显示


无法进入打ze模式

unable to enter into doze mode




  1. 此后,我在距当前时间1小时1分钟后设置了闹钟,使我的设备进入打ze模式,然后成功进入打ze模式。然后我什么也没做,即使它处于打mode模式,它也能准时发出警报。

因此,我得出结论如果当前时间和您的警报时间之间的时间戳很短,则setAlarmClock()方法可防止您的设备进入打do模式。否则,如果您的设备已经处于打ze模式,那么它将在打some时间之前退出打mode模式,因此,您的闹钟可以正常工作。

Hence, i conclude that setAlarmClock() method prevents your device from entering to doze mode if there is a small amount of timestamp between current time and your alarm time. Otherwise if your device is already in doze mode then it will gets exit from doze mode before some time of your alarm so, your alarm works fine.

更新的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(d.getTime(),pendingIntent),pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    alarmManager.setExact(AlarmManager.RTC, d.getTime(), pendingIntent);
else
    alarmManager.set(AlarmManager.RTC, d.getTime(), pendingIntent);

这篇关于警报管理器的setExactAndAllowWhileIdle()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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