Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel alarm from AlarmManager

Tags:

android

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.

like image 225
Vladimir Homola Avatar asked Mar 08 '15 02:03

Vladimir Homola


People also ask

How do I stop AlarmManager?

In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.

How do I cancel all my alarms?

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.

What is alarm Manager?

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.

How do I set multiple alarms on Android?

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.


1 Answers

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); 
like image 189
Daniel Nugent Avatar answered Oct 07 '22 19:10

Daniel Nugent