AlarmManager每2分钟执行一个任务,实际却是每2的整数倍执行

最近想实现一个功能,及在锁屏状态下能每隔2分钟启动service,执行一个任务。google了好多大牛的博客,也没有得到理想的解决方法。我对系统理解比较浅,望大牛们能指点迷津。

要求实现AlarmManager每2分钟执行一个任务,可实际关闭屏幕测试却是每2的整数倍执行我的任务。为了监视我的程序,我写了一个在sdcard写log的函数,用来监视service是否按需求在干活。

关键代码部分:
这部分其实是Sample里面的例子,继承一个Activity,在里面实现:
 // Activity中的代码;设置每2分钟启动,在Button点击后启动
            long firstTime = SystemClock.elapsedRealtime();
            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 2*60*1000, mAlarmSender);


下面这部分:在Service中实现,在sdcard写监测log记录,目的是为了检测service中任务是否按每2分钟运行。
ReadAndWrite().WriteTxtFile()是我写的一个在sdcard写字符的函数。
// 获取当前时间(精确到毫秒), 格式为: yyyy-mm-dd hh:mm:ss.nnn
String currentTime = new java.sql.Timestamp(System.currentTimeMillis()).toString();
new ReadAndWrite().WriteTxtFile(
Environment.getExternalStorageDirectory()
+ "/MyAlarmManager.log",
currentTime
+ ":am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 2*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第"
+ (++count) + "次");

startMyActivity("com.austin.myalarmmanager",
"com.austin.myalarmmanager.PopUpDisplay");



3、程序测试结果输出,这是我从Sdcard中的记录文件复制来的,请大拿们看记录的分钟时间间隔,我现在问的是怎么是每隔2分钟的整数倍执行任务,怎么样才能达到我的需求。另外改怎么样才能实现需要的时候拿到cpu唤醒锁,不用的时候还给系统,不给设备增加无用的功耗?下面是测试记录:
2013-09-23 02:22:18.785:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第20次
2013-09-23 02:24:18.788:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第21次
2013-09-23 02:25:45.717:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第22次
2013-09-23 02:29:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第23次
2013-09-23 02:33:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第24次
2013-09-23 02:37:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第25次
2013-09-23 02:41:45.719:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第26次
2013-09-23 02:45:45.716:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第27次
2013-09-23 02:49:45.725:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第28次
2013-09-23 02:53:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第29次
2013-09-23 02:57:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第30次
2013-09-23 03:01:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第31次
2013-09-23 03:05:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第32次


中间省略一段都是每隔4分钟的记录
以下又从2分钟正常情况转到4分钟,2分钟时候我屏幕开启,所以正常
2013-09-23 07:41:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第89次
2013-09-23 07:43:45.71:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第90次
2013-09-23 07:45:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第91次
2013-09-23 07:47:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第92次
2013-09-23 07:51:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第93次
2013-09-23 08:13:45.716:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第94次
2013-09-23 08:17:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第95次
2013-09-23 08:21:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第96次
2013-09-23 08:25:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第97次
2013-09-23 08:29:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第98次
2013-09-23 08:33:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第99次
2013-09-23 08:37:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第100次


下面一段是凌晨3点到4点的,我保证我没有摸起来coding,所以屏幕一直是关闭
2013-09-24 03:03:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第343次
2013-09-24 03:07:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第344次
2013-09-24 03:11:45.711:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第345次
2013-09-24 03:15:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第346次
2013-09-24 03:19:45.71:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第347次
2013-09-24 03:23:45.716:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第348次
2013-09-24 03:27:45.723:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第349次
2013-09-24 03:31:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第350次
2013-09-24 03:35:45.711:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第351次
2013-09-24 03:39:45.769:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第352次
2013-09-24 03:43:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第353次
2013-09-24 03:47:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第354次
2013-09-24 03:51:45.715:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第355次
2013-09-24 03:55:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第356次
2013-09-24 03:59:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第357次
2013-09-24 04:03:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第358次
2013-09-24 04:07:45.719:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第359次
2013-09-24 04:11:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第360次
2013-09-24 04:15:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第361次
2013-09-24 04:19:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第362次
2013-09-24 04:23:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第363次
2013-09-24 04:27:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第364次
2013-09-24 04:31:45.716:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第365次
2013-09-24 04:35:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第366次
2013-09-24 04:39:45.713:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第367次
2013-09-24 04:43:45.773:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第368次
2013-09-24 04:47:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第369次
2013-09-24 04:49:45.71:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第370次
2013-09-24 04:53:45.714:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第371次
2013-09-24 04:57:45.712:am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, TIME*60*1000, mAlarmSender);(每隔2分钟)启动Service一次,本次为第372次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值