无法通过广播接收器取消警报

时间:2017-03-13 16:36:16

标签: android notifications broadcastreceiver alarmmanager repeatingalarm

我正在尝试取消特定日期的重复通知,因此我将其设置在广播接收器中,该接收器在该特定时间触发,通过该唯一ID取消重复通知。但它没有取消通知。我尝试了下面提到的两种技术。

mainActivity的代码,我在设置闹钟。

                        Log.v("setting range alarm","key range id = " + keyIds[j] + "time = " + RangeTimes[j]);
                    // Setting the alarm for repeating notifications, with different broadcast Receiver (alertIntent).
                        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, RangeTimes[j], AlarmManager.INTERVAL_DAY,
                                PendingIntent.getBroadcast(this, keyIds[j], alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));  // for exact repeating THIS

                        Log.v("setting range alarm","key range id = " + keyIds[j] + "Cancel time = " + CancelRangeTimes[j]);


                        // Setting the alarm for cancellin the repeating notifications, with different broadcast Receiver(cancelIntent).

                        cancelIntent.putExtra("CancelID", keyIds[j]);
                        cancelIntent.putExtra("key", pendingIntent);

                        AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                        alarmManager1.set(AlarmManager.RTC_WAKEUP, CancelRangeTimes[j],
                                PendingIntent.getBroadcast(this, keyIds[j], cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT));  // for exact repeating THIS

这是取消广播接收器即使在正确的时间被呼叫也无法正常工作。

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


        String pleaseText = intent.getStringExtra("text");
      // Methid # 1 ( By getting the Alarm id through intent and trying to cancel on the same id
/*
        int cancelReqCode = intent.getIntExtra("CancelID", 0);
        Log.v("setting the cancel", "inside broadCast reciver " + cancelReqCode);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, cancelReqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.v("setting the cancel", "inside broadCast reciver " + pendingIntent);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);

*/

        // Methid # 2 

        Log.i("okk", "please text insode cancel" + pleaseText);


        PendingIntent pendingIntent = intent.getParcelableExtra("key");
        Log.v("setting the cancel","inside broadCast reciver " + pendingIntent  );
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);



    }

//下面是mainActvity的代码,我在其中设置了alaram管理器,alertIntent,CancelIntent和Pending Intents。我已经省略了获取数组值并设置它们的代码,因为它在这里没用,但是每个日期值都是正确的。这个setAlarm()是在onCreate方法中调用的。

   public class MainActivity extends ActionBarActivity implements  AsyncResponse, View.OnClickListener {

    AlarmManager alarmManager;
PendingIntent pendingIntent;



        public void setAlarm1() {

    long[] RangeTimes = new long[C.getCount()];
    long[] CancelRangeTimes = new long[C.getCount()];
    String rangeCheck = "false" ;
    int[] KeyRangeIds = new int[C.getCount()];

    Intent alertIntent = new Intent(this, AlertReceiver.class);
    Intent cancelIntent = new Intent(this, CancelAlarmBroadcastReceiver.class);

    alertIntent.putExtra("strings", DealTimes);  
    cancelIntent.putExtra("strings", DealTimes);


    // ------------------------- SETTING ALARM --------------------------------


    for (int i = 0; i < UserFoodType.length; i++) {


        for (int j = 0; j < C.getCount(); j++) {


                RangeTimes[j] = calendar.getTimeInMillis();


                CancelRangeTimes[j] = calendar1.getTimeInMillis();

            }


            if(FoodType[j].equals(UserFoodType[i]))
            {

                for (int k = 0; k < UserDealZone.length; k++) {


                    if(DealZone[j].equals(UserDealZone[k]))
                    {



                            Log.v("setting range alarm","key range id = " + keyIds[j] + "time = " + RangeTimes[j]);

                            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, RangeTimes[j], AlarmManager.INTERVAL_DAY,
                                    PendingIntent.getBroadcast(this, keyIds[j], alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));  // for exact repeating THIS

                            Log.v("setting range alarm","key range id = " + keyIds[j] + "Cancel time = " + CancelRangeTimes[j]);
                            //    alertIntent.putExtra("cancelID", keyIds[j]);

                            cancelIntent.putExtra("CancelID", keyIds[j]);
                            cancelIntent.putExtra("key", pendingIntent);

                            AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                            alarmManager1.set(AlarmManager.RTC_WAKEUP, CancelRangeTimes[j],
                                    PendingIntent.getBroadcast(this, keyIds[j], cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT));  // for exact repeating THIS

            }

        }
    }

1 个答案:

答案 0 :(得分:0)

你在cancelIntent加了一个额外的东西,就像这样:

cancelIntent.putExtra("key", pendingIntent);

但是,pendingIntent永远不会被初始化,因此它将是null

调用CancelAlarmBroadcastReceiver.onReceive()时,此代码:

PendingIntent pendingIntent = intent.getParcelableExtra("key");

可能正在返回null。您正在记录,您应该能够检查。

在任何情况下,如果pendingIntent为空,则调用AlarmManager.cancel()将不会执行任何操作。