到目前为止,我有这种设置重复警报的方法。这个闹钟在上午11点15分以15分钟的间隔发出警报。我想知道我是否想要在上午12:00之前设置另一个重复闹钟以及此闹钟,我是否需要做任何不同的事情?
此外,是否可以在警报响起时显示多种视图?如果我想在上午11:15弹出一个视图,并在下午12:36弹出一个不同的视图,我可以设置它吗?如果是这样,怎么样?
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();
}
}
答案 0 :(得分:1)
我刚刚写完了关于这个主题的博客文章。您需要处理触发警报时要执行的操作的所有逻辑以及类中扩展BroadcastReceiver的代码。
希望这很清楚,但如果您还有其他问题,我可以更新答案。