Android:AlarmManager每天午夜一次重述任务

编程入门 行业动态 更新时间:2024-10-24 19:28:32
本文介绍了Android:AlarmManager每天午夜一次重述任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在每天结束时(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每天午夜一次重述任务

本文发布于:2023-11-27 05:19:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:午夜   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!