每天在特定时间运行代码-AlarmManager [英] Run code every day at a specific time - AlarmManager
问题描述
当前,我正在尝试在特定时间运行一段代码。经过研究,我认为正确的方法是使用 AlarmManger
。该代码应每天凌晨3点执行。如果电话在凌晨3点关机,则应在打开电话后立即执行代码。
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.
我用googled搜索后发现了很多结果。但是我的代码无法正常工作。
I used googled and found a lot of results. But my code isn't working correctly.
昨天我收到了通知。通知时间是晚上10点。但是在代码中,时间设置为凌晨3点。随着时间的推移,我设置了很多警报管理器(因为进行了测试)。触发的AlarmManager是否可能是旧的?注意:在设置新的AlarmManager之前,我从手机中删除了完整的应用程序,然后重新安装了该应用程序。 (我认为这会删除所有已设置的AlarmManager吗?)
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);
此代码将在我的应用每次启动时执行(在 MainActivity的
onCreate()内;
方法)。
This code will be executed on every startup of my app (inside MainActivity's
onCreate();
method).
推荐答案
注意:从API 19开始,所有重复的警报都是不精确的。如果您的应用程序需要精确的交付时间,则它必须使用一次精确的警报,每次都重新计划。
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.
不精确的调度意味着警报的任何两个连续触发可能会有所不同。我想这就是您所遇到的情况。要克服确切的时间问题,我认为您应该使用一键警报。但是在这种情况下,您需要将其重新安排到第二天。等等。
您可以从文档 setrepeating , setInexactRepeating 。
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.
编辑:-优化DOZE模式
如果您使用的是 setAndAllowWhileIdle()
或 setExactAndAllowWhileIdle()
,
,那么您必须阅读< a href = https://developer.android.com/training/monitoring-device-state/doze-standby rel = nofollow noreferrer>本文档表示:-
- TO optimizing DOZE mode
In Case you are Using setAndAllowWhileIdle()
or setExactAndAllowWhileIdle()
,
Then you must read This Document which says :-
对于每个应用,setAndAllowWhileIdle()和setExactAndAllowWhileIdle()均不能每9分钟触发一次警报。
Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 minutes, per app.
这篇关于每天在特定时间运行代码-AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!