我正在尝试在特定日期安排一些本地通知,而我正在使用AlarmManager来安排它们。不幸的是,它在将来的某些时候不起作用。有什么想法吗?
这是我计算触发警报的时间
private long computeDelay(int day){
long currentSystemTime = System.currentTimeMillis();
Calendar scheduleTime = Calendar.getInstance();
// used to compute number of hours and mins
int currentDay = scheduleTime.get(Calendar.DAY_OF_MONTH);
scheduleTime.set(Calendar.DAY_OF_MONTH, currentDay + day);
scheduleTime.set(Calendar.HOUR, 7);
scheduleTime.set(Calendar.MINUTE, 0);
scheduleTime.set(Calendar.SECOND, 0);
scheduleTime.set(Calendar.AM_PM, Calendar.PM);
return scheduleTime.getTimeInMillis();
}
这就是我安排闹钟的方式。
private void scheduleNotification(Notification notification, long time) {
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}
上下文是应用程序(即getApplication())。
非常感谢!
答案 0 :(得分:0)
问题在于我如何获得PendingIntent。
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
我多次调用警报计划方法,请求代码始终为0,因此只安排最后一次警报。我现在给每个警报计划一个唯一的请求代码,它似乎工作正常。