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 宜未雨而绸缪,毋临渴而掘井.----朱用纯<治 ...
随机推荐
- 设计模式——工厂模式 (C++实现)
软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径.设计模式中运用了面向对象编程语言的重要特性:封装.继承.多态,真正领悟设计模式的精髓是可能一个漫长的过程,需要大量实践经验的积累. ...
- vs2010 Express 下载连接
记录: vs2010 Express 下载连接 正式下载链接: http://download.microsoft.com/download/5/C/1/5C156922-CA10-49D8-B7E ...
- find grep使用
-------------------------------------find---grep---------------------------------------- 在当前目录下所有文件中 ...
- Oracle创建存储过程、执行存储过程基本语法
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- input框中的背景文字
<input placeholder="入库单单号" type="text" ><button>查找</button>
- css动画结束后 js无法修改translated值 .
由于项目的需要,俺要做一些页面的转场动画. 即将是移动端,肯定是首先css动画了. 结果确发现,css动画中,如果设置animation-fill-mode: both;在动画结束后无法个性trans ...
- 20、CSS
CSS 层叠样式表(Cascading Style Sheets). 用于定义显示HTML样式. DIV和SPAN div是块级元素. span是行级元素. 将一些页面中的内容包裹起来统一设置样式. ...
- phpstrtotime()对于31日求上个月有问题
PHP自带的strtotime()对于31日求上个月有问题,如下: <?php $date = "2012-07-31"; $date_unix = strtotime($d ...
- [XML] C#ResourceManagerWrapper帮助类 (转载)
点击下载 ResourceManagerWrapper.rar /// <summary> /// 类说明:ResourceManagerWrapper /// 编 码 人:苏飞 /// ...
- oracle数据库误删恢复方法
一.如果只是误删部分数据或者某条数据可以通过 1.select * from 误删除的表明 as of timestamp to_Date('恢复年月日 时分秒', '恢复时间格式') ...