The PendingIntent needs to be created exactly as it was when starting AlarmManager, and it seems that the main problem is that you are using a different requestCode (zero instead of one).
For a quick fix, this should work:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); alarmManager.cancel(pendingIntent);
To use the PendingIntent.FLAG_UPDATE_CURRENT flag, see below:
Installation:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
Cancel
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);
Daniel Nugent
source share