Search code examples
javaandroidalarmmanager

Unable to cancel PendingIntent from AlarmManager


I am new to Android, and i am creating Date Reminder app where user will enter date, time and message to be notified on the entered date & time. Till now i have achieved Creating Alarm, Showing Notification, On Click of notification, opening the App with details of that notification and update if required, Listing the All the Alarm (I used SqlLite to store data).

On my Alarm Listing Page, On click of item, i am showing AlertDialog box with option to delete, when i click on delete i am deleting the Alarm details from database, but unable to cancel the alarm.

Here is the code from 2 classes, one where i am creating the alarm, and other where i am cancelling the alarm, not sure what i am missing!!!!

Class CreateActivity extends Activity
methode setAlarm{
.....
        Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        db = new DatabaseHandler(this);
        if(!isNotified) //create new alarm
        {
            Random random = new Random();
            alarmId  = random.nextInt(10000);
            pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
            db.addReminder(pojo);
            System.out.println("Adding Reminder.");
        }else{ // Its from notification
            pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
            db.updateReminder(pojo);
            System.out.println("Updating Reminder.");
        }

        myIntent.putExtra("alarmId", alarmId);

        pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);

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

Here AlarmId is unique passed to PendingIntent

Here is another List activity where on click on AlertDialog i am deleting the Alarm from database and trying to cancel the Alarm manager. Here i used same AlarmId which was used to create the PendingIntent

Class MyListActivity extends ListActivity
....
private void deleteReminder(int alarmId) {
    System.out.println("alarmId= "+ alarmId);
    DatabaseHandler db = new DatabaseHandler(this);
    DateBookPojo pojo = new DateBookPojo();
    pojo.setReminderId(alarmId);
    db.deleteReminder(pojo); // delete alarm details from database
    System.out.println("Deleted Entry. = > " + alarmId);

    Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    System.out.println("Cancelled Alarm. = > " + alarmId);

    showList(); // Again show the new list.
}

I googled & stackoverflow a lot and but everywhere i saw this solution working fine, but its not working for me.

One think i noted here, Onlick of notification, i am using the CreateActivity class to show details and when updated, its updating the notified alarm. I guess i am messing something around the Context related to Intent & PendingIntent.

Please help me.


Solution

  • Add this:

    myIntent.putExtra("alarmId", alarmId);
    

    to your deleteReminder() method (or remove it from setAlarm).

    In order to remove a PendingIntent it has to match exactly.