这是我到目前为止的代码
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秒(进入下一分钟)。我不确定如何使用日历功能来实现这一目标?
答案 0 :(得分:4)
如果我了解您的要求,那么您可以尝试以下内容......
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);
答案 1 :(得分:0)
如果您查看AlarmManager
的文档,则表示RTC_WAKEUP
使用的时间相对于System.currentTimeMillis()
:
RTC_WAKEUP System.currentTimeMillis()中的闹钟时间(以UTC为单位的挂钟时间),它会在设备熄灭时唤醒设备。
只需修改您的triggerAtTime
参数,例如立即开始:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15 * 1000, pendingIntent);
然后警报将每15秒重复一次。