2016-11-15 64 views
0

我想在特定日期安排一些本地通知,并且我使用AlarmManager来安排它们。不幸的是,它不适用于未来的时间。任何想法为什么?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())。

非常感谢!

+2

删除您的图片和粘贴代码。图像不可搜索。 – 323go

+1

排序!对于那个很抱歉! –

回答

0

问题在于我如何获得PendingIntent。

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

我正在多次调用报警计划方法,并且请求代码始终为0,因此它只安排最后一次报警。我现在给每个报警日程安排一个唯一的请求代码,它似乎工作正常。