2016-11-15 60 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,因此它只安排最後一次報警。我現在給每個報警日程安排一個唯一的請求代碼,它似乎工作正常。