取消已使用AlarmManager和BroadCast Reciever设置的通知

时间:2016-07-20 19:03:02

标签: android sqlite notifications broadcastreceiver alarmmanager

在我的活动中,我设置了AlarmManager的时间,以便在将来的日期生成通知,并将信息存储在sqlite db中。通过更改ids设置多个通知以通知notificationManager后,我在ListView中显示信息。

现在我要取消或删除一些通知。例如,我在1小时后设置通知时间,我想在列表视图的longitemclick上取消它,但会生成通知。

我看到了如何取消通知栏中的通知但不适合我的情况的方法。我想在生成之前取消通知。

我怎样才能实现这个目标?

这是我的通知代码:

static char s3[100]

MYRECIEVER CLASS:

custom = new CustomDateTimePicker(noticall.this,new CustomDateTimePicker.ICustomDateTimeListener() {

               @Override
               public void onCancel() { 
                   finish();
               }


               @Override
               public void onSet(Dialog dialog, Calendar calendarSelected,Date dateSelected, int year, String monthFullName,
                            String monthShortName, int monthNumber, int date,
                            String weekDayFullName, String weekDayShortName,
                            int hour24, int hour12, int min, int sec,
                            String AM_PM) {

                      Calendar calendar =  Calendar.getInstance();
                      calendar.set(year, monthNumber, date, hour24,  min, 0);

                      long when = calendar.getTimeInMillis();         // notification time


                      myIntent = new Intent(noticall.this, MyReceiver.class);
                      pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);

                      alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                      alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);


        Date = calendarSelected.get(Calendar.DAY_OF_MONTH) + "/" + (monthNumber+1) + "/" +year; 
        Time = hour12 + ":" + min + " " + AM_PM;


        setlisto(Date);
        Intent intent = getIntent();
        finish();
        startActivity(intent);    

        Toast.makeText(noticall.this,"Record Added", Toast.LENGTH_SHORT).show();

           }
    });      

实用工具类:

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        /*Intent service1 = new Intent(context, MyAlarmService.class);
         context.startService(service1);*/

        Log.i("App", "called receiver method");
        try{
            Utils.generateNotification(context);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

在我的此活动中,我想取消设置通知:

public class Utils {

    //int k = noticall.in2;

    static int i=0;
     static long[] vibrate = {  1000, 1000, 1000, 1000, 1000 };
     public static NotificationManager mManager;

    @SuppressWarnings("static-access")
    public static void generateNotification(Context context){ 




        mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        int j=i+=1;
        Intent intent1 = new Intent(context,Sipdroid.class);
        Notification notification = new Notification(R.drawable.yel1," NOTIFICATION!", System.currentTimeMillis());
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, "xxxx", "xx "+noticall.Names, pendingNotificationIntent);
        Log.i("increeeee", noticall.incre+"");
        mManager.notify(j, notification); // for multiple notifications... change 0 to no of notification.... 
    }

}

我试过这段代码,但是它可以取消所有待取消的意图取消我想要取消特定的pendingIntents:

this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
                    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                        Log.w("DELETED", " DELETED");

        // code here to delete specific notification or pendingIntent or alarmmanager


   }
                });   

如何取消特定的pendingIntents?

2 个答案:

答案 0 :(得分:1)

您无法从警报管理器获取实际的待处理意图,但您可以取消它。 使用您之前使用的完全相同的数据和id重新创建intent,然后将intent传递给AlarmManager的取消功能,这将允许您取消通知。

<div>
    <uib-accordion>
        <uib-accordion-group is-open="true">
            <uib-accordion-heading>
                {{vm.moduleMenu.name}}<i class="pull-right glyphicon"></i>
            </uib-accordion-heading>

            <div>... other content...</div>
        </uib-accordion-group>
    </uib-accordion>
</div>

其中pendingIntent是您要取消的意图。

参考的一些链接: - How to get and cancel a PendingIntent?

Cancelling a PendingIntent

答案 1 :(得分:0)

好吧,我自己解决了我的问题。我们可以管理多个pendingIntent

 public void setAlarm(long timu,int id2){

              myIntent = new Intent(noticall.this, MyReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(noticall.this, id2, myIntent,0);

              alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
              alarmManager.set(AlarmManager.RTC, timu, pendingIntent);


        }


        public void unsetAlarm(View v,int id) {


              myIntent = new Intent(noticall.this, MyReceiver.class);
              pendingIntent = PendingIntent.getBroadcast(noticall.this, id, myIntent,0);

              alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

              alarmManager.cancel(pendingIntent);

       } 

我在创建pendingIntents时分配了相同的id,并且为了删除我读取了分配的id并取消了alarmManager,以便在触发之前关闭通知。

作为`pendingIntent = PendingIntent.getBroadcast(this,id,myIntent,0);

id是PendingIntent的requestCode,我们可以跟踪它并取消所需的PendingIntent,只需传递你想要取消的id。

可能对某人有帮助。干杯。快乐的Android编码。