Android:AlarmManager每天午夜一次重述任务 [英] Android: AlarmManager recuring tasks once a day at midnight
问题描述
我试图在每天结束时(23.59)重复执行一项任务,所以我想使用alarmmanager.setRepeating()。
在我的测试中,我想出了一些代码,我以为每秒可以重复执行一个任务,它可以正常工作,但不是每秒钟重复一次,而是每分钟重复一次任务,我不确定为什么,因此我不知道确保我将使它每天都能工作。这是我的代码:
I am trying to make a task repeat at the end of each day (23.59) to do so I would like to use alarmmanager.setRepeating(). In my test i have come up with a bit of code that i thought would repeat a task every second, it works but instead of being everyseconds it repeats the tasks every minute, I am not sure why and therefore I am not sure that I will be able to make it work for every day. Here is my code:
public class RecurringTasks extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// add calculation logic here
Toast.makeText(context, "happening !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
}
public void setRecurringTasks(Context context)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, RecurringTasks.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000, pi);
}
public void cancelRecurringTasks(Context context)
{
Intent intent = new Intent(context, RecurringTasks.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
这是导致问题的行,我认为:
This is the line that is causing problem i think:
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000, pi);
我以为1000毫秒?因此应该是1s?
I thought the 1000 was in miliseconds ? it should therefore be 1s ?
这是我在日志中看到的消息:
Here is the message I get in my logs:
D/DEBUG: im repeating
E/EGL_emulation: tid 3322: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9773e540, error=EGL_BAD_MATCH
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb2c768a0
解决方案
Android不允许您在不到一分钟的时间内重复执行任务,这就是为什么我只在每分钟重复一次。为了使它每天在23.59.59重复,我使用了以下代码:
Android will not allow you to repeat a task in less then one minute this is why mine was only repeating every minute. To manage to make it repeat every day at 23.59.59 I used the following code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, RecurringTasks.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
推荐答案
我以为1000是几毫秒?因此应该是1s?
I thought the 1000 was in miliseconds ? it should therefore be 1s ?
是。但是,在Android 5.1及更高版本上,您无法安排经常重复执行任务。如果您尝试这样做,则将舍入到一分钟。
It is. However, on Android 5.1 and higher, you cannot schedule a repeating task that frequently. If you try, it will be rounded up to a minute.
这篇关于Android:AlarmManager每天午夜一次重述任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!