【问题标题】:AlarmManager Interval for AndroidAndroid 的 AlarmManager 间隔
【发布时间】:2012-06-05 21:40:55
【问题描述】:

这是我目前的代码

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setRepeatingAlarm();

public void setRepeatingAlarm() {

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

    Intent intent = new Intent(this, TimeAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), (15 * 1000), pendingIntent);
  }

}

这就是我想要完成的全部任务:闹钟要到每分钟过去 30 秒才会打开。一旦你清除它,它会在 下一分钟 分钟后 30 秒后重新启动。因此,如果我打开应用程序,并且是每分钟 25 秒,它将在 5 秒后激活状态栏通知。但是如果已经过去了 40 秒,我将不得不再等 50 秒(到下一分钟)。我不知道如何使用日历功能来实现这一点?

【问题讨论】:

    标签: android notifications alarmmanager statusbar


    【解决方案1】:

    如果我理解您的要求,那么您可以尝试以下方法...

    Calendar cal = Calendar.getInstance();
    if (cal.get(Calendar.SECOND) >= 30)
        cal.add(Calendar.MINUTE, 1);
    cal.set(Calendar.SECOND, 30);
    
    // Do the Intent and PendingIntent stuff
    
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 1000, pendingIntent);
    

    【讨论】:

      【解决方案2】:

      如果您查看AlarmManager 的文档,它会说RTC_WAKEUP 使用时间相对于System.currentTimeMillis()

      RTC_WAKEUP System.currentTimeMillis() 中的闹钟时间(UTC 挂钟时间),它会在设备关闭时唤醒设备。

      所以只需修改您的triggerAtTime 参数,例如立即开始:

      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15 * 1000, pendingIntent);
      

      然后警报将每 15 秒重复触发一次。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多