AlarmManager.setRepeating将不再准确
背景:
Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.
With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requesteddelivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).
原来,操作系统为了节能省电,将会调整alarm唤醒的时间。故通过AlarmManager.setRepeating()方法将不再保证你定义的工作能按时开始。
解决办法:
1. 按照官方网站说的,调用API 19之后出现的setWindow()或者setExact()方法。
2. 按照如下方法写scheduleAlarm()方法:
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD = ; // 1 minutes
@Override
public void onReceive(Context ctxt, Intent i) {
if (i.getAction().equals("great")) {
scheduleAlarms(ctxt);
//Do your work
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, PollReceiver.class);
i.setAction("great");
PendingIntent pi = PendingIntent.getBroadcast(ctxt, , i, );.
mgr.cancel(pi);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+PERIOD, pi);
}
}
AlarmManager.setRepeating将不再准确的更多相关文章
- Android 开发 AlarmManager 定时器
介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
- android AlarmManager 详解
在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接.怎么完成心跳很关键,在说道客户端心跳功能时,如果使用Timer或者专门开起一个线程来做心跳的工作,会浪费CPU工作时间,而且也会更多 ...
- AlarmManager守护服务和隐藏桌面图标
1.主要内容 本章记录几段常用代码: 1.如何使用AlarmManager守护服务2.如何判断某服务是否正在运行 2.如何暂时禁用Android的组件 2.使用AlarmManager守护服务 Boo ...
- Android之AlarmManager
Android平台中,Alarm Manager Service控制着闹钟和唤醒功能.和其他系统服务一样,提供了一个辅助管理类-AlarmManager,我们只需要使用AlarmManager即可调用 ...
- 我的Android进阶之旅------>Android使用AlarmManager全局定时器实现定时更换壁纸
该DEMO将会通过AlarmManager来周期的调用ChangeService,从而让系统实现定时更换壁纸的功能. 更换壁纸的API为android.app.WallpaperManager,它提供 ...
- Android高级第十一讲之不同系统间的区别
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Android系统不断的升级,从基础到中级再到高级,逐步升级是软件工程敏捷开发的一个重点,在每个版本 ...
- android离线下载的相关知识
离线下载的功能点如下: 1.下载管理(开始.取消下载). 2.网络判断(Wi-Fi,3G). 3.独立进程. 4.定时和手机催醒. 5.自启动. 选择 ...
- 【转】Android学习系列–App离线下载功能实现
原文:http://www.cnblogs.com/qianxudetianxia/archive/2011/07/20/2108965.html 宜未雨而绸缪,毋临渴而掘井.----朱用纯<治 ...
随机推荐
- Java基础知识强化之集合框架笔记62:Map集合之HashMap嵌套HashMap
1. HashMap嵌套HashMap 传智播客 jc 基础班 陈玉楼 20 高跃 ...
- Android界面的View以及ViewGroup的区别
因为这个问题会经常成为面试的热点,所以我们来谈谈View以及ViewGroup的区别. 先看看View及ViewGroup类关系 Android View和ViewGroup从组成架构上看,似乎 ...
- RedHat7搭建PHP开发环境(Zend Studio)
下载Zend Studio # wget http://downloads.zend.com/studio-eclipse/13.0.1/ZendStudio-13.0.1-linux.gtk.x86 ...
- C# addin 开发心得记录
1.环境 2012 新建项目-2010外接程序 2.新建项-功能区 创建菜单等 发布: 1.InstallShield 2015 2.打包 说明按地址-https://msdn.microsoft ...
- [Mime] MimeHeaders--MimeHeader帮助类 (转载)
点击下载 MimeHeaders.rar 这个类是关于Mime的Headers类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 // ...
- css - div垂直方向滚动
只要设置 OVERFLOW-Y:auto;OVERFLOW-X:hidden即可.
- Orcale安装完成后 em管理、性能无法登陆 报:没有找到主机
先在我的电脑环境变量中加入oracle_sid=orcl 在Orcale主目录中查找emd.properties 文件修改(时间格式) agentTZRegion=GMT agentTZRegion= ...
- Ubuntu下解压rar文件的方法
原帖地址:http://hi.baidu.com/remoteexp/item/1c32d0ffb92e946c3c148596 一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了ra ...
- oracle数据库误删恢复方法
一.如果只是误删部分数据或者某条数据可以通过 1.select * from 误删除的表明 as of timestamp to_Date('恢复年月日 时分秒', '恢复时间格式') ...
- html5 + css3 + zepto.js实现的微信广告宣传页
最新学习html5 + css3, 参考微信的一个推广页写出一个实例巩固自己知识,自己已经将原实例打包到自己博客文件当中,但是不知道如何提供下载,如有需要的朋友可以联系我qq309666726