Tarika Chawla
Tarika Chawla

Reputation: 133

Not able to cancel Pending Intent using Alarm manager

This is the code to set u and delete the notifications. please let me know if you need more details. The only solutions on stack overflow are about the pending intent to be same. I already tried that solution and it didn't work.

public void setAlarm () throws java.text.ParseException {
    RealmResults<Reminder> realmResults = getAllLatest();

    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Reminder reminder =  getLatestReminder(realmResults);
    int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
    int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
    PendingIntent pendingIntent  = getPendingIntentFromReminder(reminder);
    Log.v("Reminder is: ", String.valueOf(reminder));
    Calendar calendar = Calendar.getInstance();
    //calendar.setTimeZone(TimeZone.getTimeZone());
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);


    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent);
}
private PendingIntent getPendingIntentFromReminder(Reminder reminder) throws ParseException {
    int hour = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getHours();
    int minutes = convertTimeToHoursAndMinutes(reminder.getReminderTime()).getMinutes();
    Intent intent = new Intent(this, MyReceiver.class);
    intent.setAction(reminder.getName());
    Uri data = Uri.withAppendedPath(Uri.parse("TYS"),
            String.valueOf(reminder.getId()));
    intent.setData(data);
    Calendar calendar = Calendar.getInstance();
    //calendar.setTimeZone(TimeZone.getTimeZone());
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);
    //Log.v("Test", calendar.getTimeZone().toString());
    // Log.v("Hours to show", String.valueOf(hour));
    // Log.v("Minutes to show", String.valueOf(minutes));
    intent.putExtra("reminderTitle", reminder.getName());
    //  Log.v("Reminder Name", reminder.getName());
    intent.putExtra("notificationType", 1);
    intent.putExtra("reminderId", String.valueOf(reminder.getId()));
    intent.putExtra("reminderSubtitle", reminder.getSubTitle());
    intent.setAction(Long.toString(System.currentTimeMillis()));
    Log.v("set Alarm", String.valueOf(intent));
    PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    return alarmIntent;
}

public void deleteReminderIntent(Reminder reminder) throws ParseException {
    Log.v("deleteReminderIntent", String.valueOf(reminder));
    AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent =getPendingIntentFromReminder(reminder);
    alarmMgr.cancel(pendingIntent);
    pendingIntent.cancel();
    //alarmMgr.cancel(temp);
   // alarmMgr.cancel(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}

Upvotes: 1

Views: 102

Answers (1)

David Wasser
David Wasser

Reputation: 95578

In getPendingIntentFromReminder() you set the ACTION in the Intent to the current time. When you call the method to get a PendingIntent to cancel your alarm, the current time is different, so the ACTION will not match the PendingIntent you have used to set the alarm.

In order to cancel the alarm, you need to use an Intent that will match the Intent that you used to set the alarm. The following parameters in the Intent must match:

  • ACTION
  • CATEGORY
  • DATA
  • COMPONENT

Any "extras" in the Intent are ignored, so they do not need to match.

For more details see https://stackoverflow.com/a/29590084/769265 and Is it possible to create multiple PendingIntents with the same requestCode and different extras?

Upvotes: 1

Related Questions