Doze模式调试下AlarmManager后台运行问题

来源:互联网 发布:周柏豪性格知乎 编辑:程序博客网 时间:2024/05/20 12:22

最近在android7.0平台下,调试在Doze模式下长连接运行状态。

从6.0开始android便加入了低耗电模式,此模式下限制了很多行为,包括我们时常用到的AlarmManager的行为限制。而对于进入Doze模式7.0去除了6.0的静止状态的条件,因此进入Doze模式比6.0更加容易

The following restrictions apply to your apps while in Doze:

  • Network access is suspended.
  • The system ignores wake locks.
  • Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
    • 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.
  • The system does not perform Wi-Fi scans.
  • The system does not allow sync adapters to run.
  • The system does not allow JobScheduler to run.
 Doze下原来的setExact()的方法几乎没用了,可用提供的新api  setAndAllowWhileIdle() 和 setExactAndAllowWhileIdle()

于是先写个demo,Alarm每分钟发送一条广播,分别使用setExact()和setExactAndAllowWhileIdle()进行测试

adb 进入Doze模式

发现setExact()设置的Alarm,几乎不起作用了。只能等到下次的maintenance window了。

而setExactAndAllowWhileIdle()设置的Alarm,只发送了一条广播便停了,这个不急再继续看官网,

Note: Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 minutes, per app.

意思是每9分钟只能起来一次了,于是继续等着,果然9分钟后,广播接收到了。

然后便开始测试长连接的状态,发现Alarm是正常使用的,并没有受到影响。

于是便想着把app和长连接一样配置为系统进程,源码下编译后从新测试,果然Alarm正常使用了。

总体来说对功耗要求越来越高的android,一般的app在 Doze模式下,只能按google要求,每9分钟只能起来一次Alarm了。





0 0
原创粉丝点击