Search code examples
androidalarmmanager

AlarmManager Alarm Not Being Deleted/Canceled. What am I doing wrong?


I'm using AlarmManager + NotificationManager to show a notification at a specific time. However, I can't quite get the alarm to get cancelled with my code. The notification/alarm keeps getting fired even after I've deleted the alarm. Can someone look through my code and check if I'm doing anything wrong? Thank you SOOOOO much! :)

This is how I'm setting my alarm/notification:

Intent intent = new Intent(this, ViewLocalReminders.class);
startActivity(intent);

//Get the primary key of the reminder that has just been saved.
Cursor cursor = remindersDAO.reminderNotification(this);
cursor.moveToLast();
int reminderIdColumnIndex = cursor.getColumnIndex("_id");
reminderPrimaryKey = cursor.getInt(reminderIdColumnIndex);

//Get the name of the reminder that has just been saved.
cursor.moveToLast();
int reminderNameColumnIndex = cursor.getColumnIndex("name");
reminderName = cursor.getString(reminderNameColumnIndex);

cursor.close();

Toast toast = Toast.makeText(context, context.getString(R.string.reminder_saved), Toast.LENGTH_LONG);
toast.show();

c.set(mYear, mMonth, mDay); //Set the notification date.
c.set(Calendar.HOUR_OF_DAY, pHour); //Set the notification hour.
c.set(Calendar.MINUTE, pMinute); //Set the notification minute.
c.set(Calendar.SECOND, 0); //Set the notification second (always 0).

//Use AlarmManager to trigger the notification/alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

//PendingIntent to launch activity when the alarm triggers.                    
Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");

//Assign the reminder's primary key as the notification ID.
i.putExtra("Reminder_Name", editRemindMeTo.getText().toString());
i.putExtra("Reminder_Primary_Key", reminderPrimaryKey);

PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), reminderPrimaryKey, i, 0);               

//Set the alarm to trigger.
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), displayIntent);

This is how I'm cancelling my alarm/notification:

Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");
PendingIntent displayIntent = PendingIntent.getService(context, (int) reminderID, i, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

alarmManager.cancel(displayIntent);
displayIntent.cancel();

So is there something that I need to change in my code in order to get it to work? Thanks! :D


Solution

  • Your pending intent is not the same while setting and canceling alarm.create same pending intent for both.Hopefully it will work.

    To cancel the alarm

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    Intent i = new Intent("com.utilityapps.Blah.DisplayReminderNotification");
    
    PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), reminderPrimaryKey, i, 0);               
    
    alarmManager.cancel(displayIntent);