-1

我需要多次创建多个通知。通知应该出现的时间被提取到 event_id 等。通知时间在另一个类中设置。下面的代码发生的情况是,对于设置在 10:00 的通知,在 10:00 之后设置的所有通知也会同时出现。请帮忙。只有正确的通知需要出现。不是未来的。

        for(int j=0;j<event_id.size();j++)
                        {
                                   if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
                            {
                                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);//alarm manager
                                NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                                Notification note=new Notification(R.drawable.friendi_main_logo, event_desc.get(j).toString(), System.currentTimeMillis());
                                Intent mainScreenIntent=new Intent(getApplicationContext(),MainScreenActivity.class);
                                mainScreenIntent.putExtra("UserID", user_id);
                                int uniqueCode=0;
                                uniqueCode= Integer.parseInt(event_id.get(j).toString());//unique code for each pending intent
                                //separate pending intent for each alarm.. one alarm manager can invoke only one PI
                                PendingIntent ListOfNotification=PendingIntent.getActivity(getApplicationContext(), uniqueCode,mainScreenIntent,0); 
                                note.flags=Notification.FLAG_AUTO_CANCEL;
                                alarmManager.set(AlarmManager.RTC_WAKEUP, Long.valueOf(event_time.get(j).toString()), ListOfNotification);//invokes pending intent @ the event_time
                                note.setLatestEventInfo(getApplicationContext(), "Event: "+event_title.get(j).toString(), event_group.get(j).toString()+": "+event_desc.get(j).toString(),ListOfNotification );
//                              Uri path=Uri.parse("android.resource://" + getPackageName() + "/alarm_sms.mp3");
//                              note.sound=path;
                                note.defaults=Notification.DEFAULT_ALL;
                                notificationManager.notify(EVENT_NOTIFY_ID, note);
                                EVENT_NOTIFY_ID++;
                                flag=true;
                            }
                        }
4

2 回答 2

0

尝试使用布尔值仅检查第一个触发。每当你准备好发射第二个时,让它成为现实。您处于 for 循环中,并且 notificationManager.notify(EVENT_NOTIFY_ID, note);每次都在调用,因此将触发所有通知。

boolean fire_only_one=true;
    for(int j=0;j<event_id.size();j++)
                            {
                                       if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
                                {
                                    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);//alarm manager
                                    NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                                    Notification note=new Notification(R.drawable.friendi_main_logo, event_desc.get(j).toString(), System.currentTimeMillis());
                                    Intent mainScreenIntent=new Intent(getApplicationContext(),MainScreenActivity.class);
                                    mainScreenIntent.putExtra("UserID", user_id);
                                    int uniqueCode=0;
                                    uniqueCode= Integer.parseInt(event_id.get(j).toString());//unique code for each pending intent
                                    //separate pending intent for each alarm.. one alarm manager can invoke only one PI
                                    PendingIntent ListOfNotification=PendingIntent.getActivity(getApplicationContext(), uniqueCode,mainScreenIntent,0); 
                                    note.flags=Notification.FLAG_AUTO_CANCEL;
                                    alarmManager.set(AlarmManager.RTC_WAKEUP, Long.valueOf(event_time.get(j).toString()), ListOfNotification);//invokes pending intent @ the event_time
                                    note.setLatestEventInfo(getApplicationContext(), "Event: "+event_title.get(j).toString(), event_group.get(j).toString()+": "+event_desc.get(j).toString(),ListOfNotification );
    //                              Uri path=Uri.parse("android.resource://" + getPackageName() + "/alarm_sms.mp3");
    //                              note.sound=path;
                                    note.defaults=Notification.DEFAULT_ALL;
    if(fire_only_one){                                
    notificationManager.notify(EVENT_NOTIFY_ID, note);
    fire_only_one=false;
    }                  
                  EVENT_NOTIFY_ID++;
                                    flag=true;
                                }
                            }


    fire_only_one=true;

此外,您可以通过在此处仅设置最相关的通知来更改逻辑,当用户通过单击第一个通知打开活动时,使用 ALARM 管理器设置第二个通知。

于 2012-04-06T07:00:20.407 回答
0

所以..你所做的是..

if(Integer.parseInt(event_id.get(j).toString())>newEventID&&Long.parseLong(event_time.get(j).toString())>System.currentTimeMillis())
  {

在这条线之后..

看到没有再次进入if循环......例如这样......

   j=event_id.size();

这只会出现一个通知..

于 2012-04-06T06:16:03.233 回答