androidnotificationsbroadcastreceiveralarmmanagerrepeatingalarm

Unable to Cancel the Alarm through broadcast Receiver


I'm trying to cancel the repeating notifications on a particular date, so I'm setting it in a broadcast receiver which is fired on that particular time cancelling the repeating notifications through that unique id. but it is not cancelling the notifications. I have tried two techniques both are mentioned below.

mainActivity's code where I'm setting the Alarm.

                        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

here is the cancelling broadcast reciver which is not working even though it is getting called on the right time.

      @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);



    }

// below is the mainActvity's code where i'm setting alaram managers, alertIntent, CancelIntent and Pending Intents. I have omitted the code where it is getting the array values and setting them because it was useless here, but every date value is correct. this setAlarm() is getting called in the onCreate method.

   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

            }

        }
    }

Solution

  • You are putting an extra in cancelIntent, like this:

    cancelIntent.putExtra("key", pendingIntent);
    

    however, pendingIntent is never initialized, so it will be null.

    When CancelAlarmBroadcastReceiver.onReceive() is called, this code:

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

    is probably returning null. You are logging that, you should be able to check.

    In any case, if pendingIntent is null, calling AlarmManager.cancel() won't do anything.