每天在特定时间运行代码 - AlarmManager [英] Run code every day at a specific time - AlarmManager
问题描述
目前,我正在尝试在特定时间运行一段代码.经过一番研究,我认为正确的方法是使用AlarmManger
.代码应该在每天凌晨 3 点执行.如果手机在凌晨3点关机,则应在开机后直接执行代码.
我用谷歌搜索并找到了很多结果.但是我的代码不能正常工作.
昨天收到通知.通知时间是晚上10点.但在代码中,时间设置为凌晨 3 点.我随着时间的推移设置了很多警报管理器(因为测试).是否有可能触发的 AlarmManager 是旧的?注意:在设置新的 AlarmManager 之前,我从手机中删除了完整的应用程序并重新安装了它.(我认为这会删除所有设置的 AlarmManager 吗?)
好的,这是我正在使用的代码:
Calendar calendar = Calendar.getInstance();calendar.setTimeInMillis(System.currentTimeMillis());日历.添加(日历.第二,0);日历.添加(日历.分钟,0);日历.添加(日历.小时,3);Intent _myIntent = new Intent(MainActivity.this, AlarmReceiver.class);_myIntent.putExtra("MyMessage","消息内容");PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, pendingIntent);
此代码将在我的应用程序每次启动时执行(在 MainActivity's
onCreate();
方法中).
注意:从 API 19 开始,所有重复的警报都是不准确的.如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次都重新安排.
不精确的调度意味着警报的任何两次连续触发之间的时间可能会有所不同.我猜你的情况就是这样.为了克服确切的时间问题,我认为你应该使用一次性警报.但在这种情况下,你需要将它重新安排到第二天.等等.您可以从文档 setrepeating , setInexactRepeating.
- 优化打瞌睡模式如果您使用 setAndAllowWhileIdle()
或 setExactAndAllowWhileIdle()
,然后你必须阅读本文档,其中说:-><块引用>
对于每个应用,setAndAllowWhileIdle() 和 setExactAndAllowWhileIdle() 都不能每 9 分钟触发一次以上的警报.
Currently, I am trying to run a piece of code at a specific time. After some research, I think the correct way to go is to make usage of the AlarmManger
. The code should be executed every day at 3 am. If the phone is at 3 am shut down, the code should be executed directly after turning the phone on.
I used googled and found a lot of results. But my code isn't working correctly.
Yesterday I got a notification. The notification time was 10 pm. But in the code, the time is set to 3 am. I set up a lot of alarm manager over the time (because of testing). Could it be possible, that the triggered AlarmManager was an old one? Note: Before setting up a new AlarmManager I deleted the complete application from my phone and installed it new. (I think this will delete all set AlarmManager's?)
Okay, here is the code I am using:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 0);
calendar.add(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 3);
Intent _myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
_myIntent.putExtra("MyMessage","Content of the message");
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, pendingIntent);
This code will be executed on every startup of my app (inside MainActivity's
onCreate();
method).
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time.
Inexact scheduling means the time between any two successive firings of the alarm may vary. Thats what happening in your case i guess.To overcome the exact time issue i think you should use one shot alarm.But in this case you need to reschedule it for next day. and so on. You can get a very clear understanding from the Documentation setrepeating , setInexactRepeating.
EDIT:- TO optimizing DOZE mode
In Case you are Using setAndAllowWhileIdle()
or setExactAndAllowWhileIdle()
,
Then you must read This Document which says :-
Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 minutes, per app.
这篇关于每天在特定时间运行代码 - AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!