-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

ブール値を使用して、最初の発火のみをチェックしてみてください。2番目のものを発射する準備ができたらいつでも真にしてください。あなたは 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 マネージャーを使用して 2 番目の通知を設定することもできます。

于 2012-04-06T07:00:20.407 に答える