2013-12-12 117 views
1

下午好。多個本地通知android

我想在我的應用程序的多個本地通知,所以我有

在我的活動

,這種方法應該設置警報,以17:20,17:21,17:22,但我只有一個17:22通知。

public void multiplyAlerts(){ 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    Intent intent = new Intent(this, LocalNotification.class); 

    for(int i = 0; i < 3; i++){ 

     Calendar t_calendar = Calendar.getInstance(); 

     t_calendar.set(Calendar.MONTH, Calendar.DECEMBER); 
     t_calendar.set(Calendar.YEAR, 2013); 
     t_calendar.set(Calendar.DAY_OF_MONTH, 12); 

     t_calendar.set(Calendar.HOUR_OF_DAY, 17); 
     t_calendar.set(Calendar.MINUTE, 20 + i); 
     t_calendar.set(Calendar.SECOND, 23); 
     t_calendar.set(Calendar.AM_PM, Calendar.PM); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, t_calendar.getTimeInMillis(),pendingIntent); 

     System.out.println("Calling Alarm " + i); 

    }} 

本地通知類

public class LocalNotification extends BroadcastReceiver { 

NotificationManager nm; 

@Override 
public void onReceive(Context context, Intent intent) { 

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    CharSequence from = "Remsmed"; 
    CharSequence message = "Принимать что-то"; 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0); 
    Notification notif = new Notification(R.drawable.launcher,"Notification text", System.currentTimeMillis()); 
    notif.setLatestEventInfo(context, from, message, pendingIntent); 
    notif.flags = Notification.FLAG_AUTO_CANCEL; 
    nm.notify((int)System.currentTimeMillis(),notif); 

    System.out.println("id = " + System.currentTimeMillis()); 
}} 

我添加此字符串來體現

<receiver android:name="ru.fors.remsmed.utils.LocalNotification"></receiver> 

感謝您的答案!

回答

5

你需要爲每一個通知一個唯一的ID:)

PendingIntent pendingIntent = PendingIntent.getActivity(context, my_id, new Intent(), 0); 
+0

(INT)System.currentTimeMillis的(是確定爲ID? – Valeriy

+0

是的,你可以使用它。 –

+0

您也可以通過一次只設置一個通知來避免這種情況。當你的17:20報警被觸發時,設置你的17:21報警等。更容易取消報警,因爲一次只有一個報警被激活。 – Kuffs