删除android中的通知

时间:2012-02-17 06:37:53

标签: android android-notifications

我在我的应用程序中添加一些通知并存储数据库中给出的数据。现在我想更新存储在数据库中的数据,我想删除先前数据的通知,为更新的数据创建通知。我正在使用报警管理器的arraylist,请求代码递增。任何人都可以帮我删除旧通知并创建新通知。如果您需要查看我将发布的代码。

for(int i = 0; i

                        if(j==0){
                                  longobjDate=objDate.getTime();
                            longobjTime=objTime1.getTime();
                            long longDay=longobjDate+longobjTime+19800000;
                            Intent intent=new Intent(New_Med.this,Alarm_Reciever.class);
                            PendingIntent pendingIntent=PendingIntent.getBroadcast(New_Med.this, k, intent, 0);
                            AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

                            alarmManagers.add(alarmManager);
                            alarmManagers.get(k).set(AlarmManager.RTC_WAKEUP, longDay, pendingIntent);

                            Toast.makeText(New_Med.this, "Alarm set", Toast.LENGTH_SHORT).show();
                            k=k+1;
                            db1.insertNotificationDetails(med_id, longobjDate, longobjTime);
                        }
                        if(j==1){
                            longobjDate=objDate.getTime();
                            longobjTime=objTime2.getTime();
                            long longDay=longobjDate+longobjTime+19800000;
                            Intent intent=new Intent(New_Med.this,Alarm_Reciever.class);
                            PendingIntent pendingIntent=PendingIntent.getBroadcast(New_Med.this, k, intent, 0);
                            AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

                            alarmManagers.add(alarmManager);
                            alarmManagers.get(k).set(AlarmManager.RTC_WAKEUP, longDay, pendingIntent);



                            Toast.makeText(New_Med.this, "Alarm set", Toast.LENGTH_SHORT).show();
                            k=k+1;
                            db1.insertNotificationDetails(med_id, longobjDate, longobjTime);

                        }
                        if(j==2){
                            longobjDate=objDate.getTime();
                            longobjTime=objTime3.getTime();
                            long longDay=longobjDate+longobjTime+19800000;
                            Intent intent=new Intent(New_Med.this,Alarm_Reciever.class);
                            PendingIntent pendingIntent=PendingIntent.getBroadcast(New_Med.this, k, intent, 0);
                            AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

                            alarmManagers.add(alarmManager);
                            alarmManagers.get(k).set(AlarmManager.RTC_WAKEUP, longDay, pendingIntent);


                            Toast.makeText(New_Med.this, "Alarm set", Toast.LENGTH_SHORT).show();
                            k=k+1;
                            db1.insertNotificationDetails(med_id, longobjDate, longobjTime);

                        }
                        if(j==3){
                            longobjDate=objDate.getTime();
                            longobjTime=objTime4.getTime();
                            long longDay=longobjDate+longobjTime+19800000;
                            Intent intent=new Intent(New_Med.this,Alarm_Reciever.class);
                            PendingIntent pendingIntent=PendingIntent.getBroadcast(New_Med.this, k, intent, 0);
                            AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

                            alarmManagers.add(alarmManager);
                            alarmManagers.get(k).set(AlarmManager.RTC_WAKEUP, longDay, pendingIntent);


                            Toast.makeText(New_Med.this, "Alarm set", Toast.LENGTH_SHORT).show();
                            k=k+1;
                            db1.insertNotificationDetails(med_id, longobjDate, longobjTime);

                        }

                    }
                    long t=objDate.getTime();
                    t=t+(24*60*60*1000);
                    objDate= new Date(t);
                }

我正在保存这样的警报管理器。我对android很新。所以请通过编码示例方式帮助我......

2 个答案:

答案 0 :(得分:1)

老兄用这个

notificationManager.cancel(NOTIFICATION_ID);  

这里NOTIFICATION_ID是int,必须定义才能在活动

中唯一标识

答案 1 :(得分:0)

查找特定通知的请求代码并使用AlarmManager类对象取消 未决的意图。 例如:

    AlarmManager alarmManager4 = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent I = new Intent(getApplicationContext(),AlarmReceiver.class);
    PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), <request_code>, I, 0);
    alarmManager4.cancel(P);
    P.cancel();

如果您取消使用警报管理器,那么它的通知也可以取消,因为它是从警报管理器启动的。