【问题标题】:Android: AlarmManager recuring tasks once a day at midnightAndroid:AlarmManager 每天午夜重复一次任务
【发布时间】:2016-12-21 21:47:28
【问题描述】:

我正在尝试在每天(23.59)结束时重复执行一项任务,因此我想使用 alarmmanager.setRepeating()。 在我的测试中,我提出了一些我认为会每秒重复一个任务的代码,它可以工作,但不是每秒重复一次任务,而是每分钟重复一次任务,我不知道为什么,因此我不确定我会能够让它为每一天工作。这是我的代码:

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);
    }
}

我认为这是导致问题的行:

am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000, pi);

我以为 1000 以毫秒为单位?因此它应该是 1s ?

这是我在日志中收到的消息:

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 每天重复,我使用了以下代码:

 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);

【问题讨论】:

  • 你的意思是 23.59?
  • 哎呀,这就是我的意思

标签: android alarmmanager repeatingalarm


【解决方案1】:

我以为 1000 以毫秒为单位?因此它应该是 1s ?

是的。但是,在 Android 5.1 及更高版本上,you cannot schedule a repeating task that frequently。如果您尝试,它将被四舍五入到一分钟。

【讨论】:

  • 好的,谢谢你,我应该在间隔中使用什么来让它每天都发生?以 23.59 为例?
  • offtop:@CommonsWare,如果不是你,谁应该回答这样的问题:D
  • @Rémi:一天有 24 小时。因此,将您的时间间隔设置为 24 小时。 INTERVAL_DAY 恰好是 24 小时内的毫秒数。要使这项工作可能在该特定时间发生,请使用设置为正确开始日期/时间的Calendar 对象,而不是System.currentTimeMillis()。然后,请注意,如果设备恰好处于打盹模式,您将无法在 Android 6.0+ 上获得控制权。
  • 这不会在 setRecurringTasks(Context context) 函数首次调用后 24 小时内完成任务吗?
  • @Rémi:如果您使用正确配置的Calendar 对象来确定第一次出现的时间,而不是System.currentTimeMillis(),则不会。
猜你喜欢
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多