I have met same issue like this . Delete alarm from AlarmManager using cancel() - Android
" I'm trying to create and delete an alarm in two different methods which are both called at different moments in the application. logic.
However when I call AlarmManager's cancel() method, the alarm isn't deleted."
In order to set :
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 1, myIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
In order to delete :
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.cancel(pendingIntent);
But this doesn't remove an alarm registered. Thanks in advance.
In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.
For Android 5.0 and up, open Clock > Alarm > select toggle next to alarm. For Android 6.0 and 6.0. 1, select the Down arrow > Dismiss.
android.app.AlarmManager. This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.
To access the alarms, tap the alarm icon at the top of the screen. To add a new alarm, tap the plus icon button at the bottom of the screen. This button can be used to add multiple alarms. To set the time for the alarm, tap the hour on the time on the left and then tap the hour on the clock on the right.
The PendingIntent needs to be created exactly as it was when you start the AlarmManager, and it looks like the main issue is that you're 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);
In order to use PendingIntent.FLAG_UPDATE_CURRENT
flag, see below:
Setting:
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);
Cancelling:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With