2012-10-21 44 views
1

我爲我的應用程序構建了通知。它的工作原理是,它在正確的時間推動,並且在按下時鏈接到正確的活動。通知不重複

但是,我把它稱爲重複鬧鐘,因爲我希望它在特定的日子裏拍攝出來。 在我最初的測試中,我將其設置爲每5秒推一次,以便快速檢查它是否正確重複。在最初推送後,一旦我清除它,通知便不會再出現。

這裏是我的代碼在我的主要活動,以建立alarmManager:

private void notificationAlarm() { 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.DAY_OF_WEEK, 1); 
    cal.set(Calendar.HOUR, 1); 
    cal.set(Calendar.MINUTE, 40); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MILLISECOND, 0); 
    long interval = cal.getTimeInMillis()+5000; 

    Intent alarmIntent = new Intent(this, alarmNotif.class); 
    PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager notifAlarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    //notifAlarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmPendingIntent); 
    notifAlarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), interval, alarmPendingIntent); 


} 

和我的廣播接收器中的代碼:

公共類alarmNotif擴展廣播接收器{

@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    String title = "Don't forget to order sushi from Arbuckle!"; 
    String subTitle = "Order before 10 AM with Arbuckle App"; 
    Intent notifIntent = new Intent(context, SecureAppStarter.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context) 
    .setContentTitle(title) 
    .setContentText(subTitle) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setWhen(System.currentTimeMillis()) 
    .setContentIntent(pendingIntent); 

    Notification notif = notifBuilder.getNotification(); 
    notifManager.notify(1, notif); 
} 

}

+0

有一點幫助嗎?沒有任何消息就能喚醒這個停泊。浪費了5個小時的睡眠! – Laurent

回答

0

我想通了。

問題是FLAG_ONE_SHOT;它應該是FLAG_UPDATE_CURRENT。

這允許通知以期望的間隔重複。

瞧!