Java 设置多个警报
到目前为止,我有一个设置重复警报的方法。这台机器在上午11点15分每隔15分钟发出一次警报。我想知道我是否想在这个闹钟旁边设置另一个12:00am的重复闹钟,我需要做些什么不同的事情吗 此外,是否可以在警报响起时显示多种视图?如果我想在上午11:15弹出一个视图,在下午12:36弹出另一个视图,我可以设置吗?如果是,怎么做Java 设置多个警报,java,android,android-alarms,Java,Android,Android Alarms,到目前为止,我有一个设置重复警报的方法。这台机器在上午11点15分每隔15分钟发出一次警报。我想知道我是否想在这个闹钟旁边设置另一个12:00am的重复闹钟,我需要做些什么不同的事情吗 此外,是否可以在警报响起时显示多种视图?如果我想在上午11:15弹出一个视图,在下午12:36弹出另一个视图,我可以设置吗?如果是,怎么做 private void setCollectionAlarms() { AlarmManager manager = (AlarmManager) getSyst
private void setCollectionAlarms() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
interval = 900000;
try {
//If the date is set to HHmm, then add current date time
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
String time = "11:15";
long timeOfFirstCollectionInMillis = format.parse(time).getTime();
System.out.println("Time in Milis: " + timeOfFirstCollectionInMillis);
Calendar now = Calendar.getInstance();
now.setTime(new Date());
Calendar cal = Calendar.getInstance();
Date timedate = format.parse(time);
cal.setTime(timedate); // thinks 1970
cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));
//If the time from the db is before now (That is no date set but time set), then set it for tomorrow
if (cal.before(now)) {
// increase
Date tomorrow = cal.getTime();
cal.setTime(tomorrow);
cal.add(Calendar.DATE, 1);
tomorrow = cal.getTime();
System.out.println("TimeDate for Tomorrow: " + tomorrow);
//convert date to milis
long timeInMilis = (tomorrow.getTime());
//Set Alarm to Repeat
manager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilis, interval, pendingIntent);
//else, set the alarm for today
} else {
timedate = cal.getTime();
System.out.println("TimeDate: " + timedate);
//convert date to milis
long timeInMilis = (timedate.getTime());
//Set Alarm to Repeat
manager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilis, interval, pendingIntent);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
我刚刚就这个话题写了一篇博文。当警报被触发并且类中扩展BroadcastReceiver的代码运行时,您将需要处理所有逻辑,以执行您想要执行的操作
希望这足够清楚,但如果您还有任何问题,我可以更新答案。请注意,在KitKat中,setRepeating的行为发生了变化-不再准确(以防万一)哦,谢谢!我需要一个准确的重复警报。这仍然可能吗?是的,
setExact()
仍能正常工作。现在有点痛苦,如果你想让它准确,你需要在处理事件时(在你的BroadcastReceiver
)从setExact设置一个新的警报,有效地产生一系列警报,最后一个警报设置下一个警报的时间。那么我应该连续设置两个警报吗?SetExact()然后使用SetRepeating(SetExact())?我可以这样嵌套它吗?实际上我在我的一个应用程序中做了类似的事情。我计划在周日写一篇关于它是如何工作的博客,写完后我可以发布一个链接(作为回答)。嗨,本,我还有一个问题,如果我想在数据库中保存报警值并将其更新为新的报警时间,我该怎么做?我如何知道何时触发报警?我想保存报警触发器和它的值,但是我怎么知道报警何时会响呢?希望这在博客文章中是清楚的?当警报触发时,您将进入广播接收器。如果要更新报警,可以先删除报警,然后再添加报警。如果这有帮助的话,我们将感谢投票。