背景:

当我们想让Android应用程序定时为做一件工作时,我们往往会在一个BroadcastReceiver中使用AlarmManager.setRepeating()方法来实现。在API 19(即Kitkat)之后,这一方法将不再准确地保证每次工作都在你设置的时间开始。
 
解释:

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将不再准确的更多相关文章

  1. Android 开发 AlarmManager 定时器

    介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...

  2. Android随笔之——闹钟制作铺垫之AlarmManager详解

    说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...

  3. android AlarmManager 详解

    在开发互联网应用时候,我们常常要使用心跳来保证客户端与服务器的连接.怎么完成心跳很关键,在说道客户端心跳功能时,如果使用Timer或者专门开起一个线程来做心跳的工作,会浪费CPU工作时间,而且也会更多 ...

  4. AlarmManager守护服务和隐藏桌面图标

    1.主要内容 本章记录几段常用代码: 1.如何使用AlarmManager守护服务2.如何判断某服务是否正在运行 2.如何暂时禁用Android的组件 2.使用AlarmManager守护服务 Boo ...

  5. Android之AlarmManager

    Android平台中,Alarm Manager Service控制着闹钟和唤醒功能.和其他系统服务一样,提供了一个辅助管理类-AlarmManager,我们只需要使用AlarmManager即可调用 ...

  6. 我的Android进阶之旅------>Android使用AlarmManager全局定时器实现定时更换壁纸

    该DEMO将会通过AlarmManager来周期的调用ChangeService,从而让系统实现定时更换壁纸的功能. 更换壁纸的API为android.app.WallpaperManager,它提供 ...

  7. Android高级第十一讲之不同系统间的区别

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! Android系统不断的升级,从基础到中级再到高级,逐步升级是软件工程敏捷开发的一个重点,在每个版本 ...

  8. android离线下载的相关知识

    离线下载的功能点如下:      1.下载管理(开始.取消下载).      2.网络判断(Wi-Fi,3G).      3.独立进程.      4.定时和手机催醒.      5.自启动. 选择 ...

  9. 【转】Android学习系列–App离线下载功能实现

    原文:http://www.cnblogs.com/qianxudetianxia/archive/2011/07/20/2108965.html 宜未雨而绸缪,毋临渴而掘井.----朱用纯<治 ...

随机推荐

  1. Elasticsearch .Net Client NEST使用说明 2.x

    Elasticsearch .net client NEST使用说明 2.x Elasticsearch.Net与NEST是Elasticsearch为C#提供的一套客户端驱动,方便C#调用Elast ...

  2. Android开发之手势滑动(滑动手势监听)详解

    Android开发之手势滑动(滑动手势监听)详解 在Android应用中,经常需要手势滑动操作,比如上下滑动,或左右方向滑动,处理手势滑动通常有两种方法:一种是单独实现setOnTouchListen ...

  3. 对RecycleView的多种item布局的封装

    本文是借鉴bingoogolapple写得BGAAdapter-Android而产生的,对此表示感谢. 效果 1.Adapter的使用 1.继承BaseAdapter 这里是我的adapter pub ...

  4. 【Android】数据库的简单应用——升级数据库

    假如我们已经创建好了一个数据库,随着功能需求的增加,想在数据库中再添加一个表,如果直接在之前的代码中插入一个表,会发现创建表失败,这是因为该数据库已经存在.该如何解决呢? 1.卸载程序,重新编译安装. ...

  5. Android 开发实践 ViewGroup 实现左右滑出窗口(二)

    接上一篇 <Android 开发实践 ViewGroup 实现左右滑出窗口(一)http://www.cnblogs.com/inkheart0124/p/3532862.html> 源码 ...

  6. linux lsof nmap netstat

    lsof -i :22    # 显示22端口当前运行的程序 lsof -c ssh  # 显示ssh进程打开的文件 lsof -p 2120  #显示进程id2120打开的文件   nmap -sP ...

  7. quartz.net 基于数据库的简单实现

    前面简单学习了通过XML配置或者内存指定的方式实现调度任务.但此用法实战用途较小,企业上多需要分布式集群的方式.quart团队也考虑到了这点,于是有了我们今天要学习的.基于数据库实现分布式. Name ...

  8. final----这篇文章是我收获很大

    final 用于声明属性.方法和类,分别表示属性不可变,方法不可重写,类不可继承. [转]Java final 修饰符知识点总结 final从字面上理解含义为“最后的,最终的”.在Java中也同样表示 ...

  9. 使用 C# 编程对RTF文档的支持

    http://www.68design.net/Development/Aspnet/Basis-AspNet/26011-1.html

  10. [XML] resources的Xml配置文件 (转载)

    <?xml version="1.0" encoding="utf-8" ?> <resources> <language> ...