在特定日期安排Android通知
我正在尝试将通知安排在特定日期:在特定日期安排Android通知,android,notifications,android-notifications,Android,Notifications,Android Notifications,我正在尝试将通知安排在特定日期: notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; Intent notificationIntent = new Intent(context, NotificationPublisher.class); notificationIntent.put
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context,
NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION,
notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long delay = millisFromNow(date);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delay,
pendingIntent);
但通知会在我安排好时间后出现。
这是未来日期的hoi计算工厂:
private static SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy HH:mm");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
return calendar.getTimeInMillis();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
怎么了?您正在使用此标志设置通知警报:
AlarmManager.ELAPSED_REALTIME_WAKEUP
正如文件所说:
SystemClock.elapsedRealtime()
(自启动以来的时间,包括睡眠),当设备关闭时将唤醒设备
因此,此标志用于在启动时触发警报,我建议您更改为:
AlarmManager.RTC
eassed\u REALTIME\u WAKEUP
读取文档。此外,long delay=millisFromNow(日期)代码>可能是从现在起到您要触发的报警的时间量?再次阅读alarmManager的文档。设置,因为它不是那样工作的。是的,很抱歉,我粘贴了错误的标志。此外,AlarmManager.RTC标志通知不会在前缀时间启动。我还注意到我的get millis函数返回一个负值。。。