Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlarmManager: is there a way to cancell ALL the alarms set?

I am building an app that set 2 alarms for each day of the week (at a certain hour and minute), the alarms repeat week after week forever.

Now the point is: if the user changes the alarms, I will need cancel the previously set alarms.

Is there a way to simply cancel all the alarms set by my application ?

like image 326
Lisa Anne Avatar asked Jul 10 '13 11:07

Lisa Anne


People also ask

How do I cancel alarm Manager?

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

How do I turn off my Android alarm?

Open your phone's Clock app . At the bottom, tap Alarm. On the alarm you want, tap the Down arrow . Cancel: To cancel an alarm scheduled to go off in the next 2 hours, tap Dismiss.

What is Android alarm Manager?

AlarmManager is a bridge between application and Android system alarm service. It can send a broadcast to your app (which can be completely terminated by user) at a scheduled time and your app can then perform any task accordingly.

How do I set alarm manager on Android?

This example demonstrates how do I implement alarm manager in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

if you are canceling previous alarms then in PendingIntent your flag should be PendingIntent.FLAG_CANCEL_CURRENT. It will prevent generating a new PendingIntent if it is already created. And make sure that before setting in alarm just cancel that same PendingIntent and after that set your alarm. You should try like this:

AlarmManager 2AlarmsInWeekAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService/getActivity(context, int, intent, PendingIntent.FLAG_CANCEL_CURRENT);

2AlarmsInWeekAlarmManager.cancel(pendingIntent);

and then you may use set or setRepeating method. In your case it should be

2AlarmsInWeekAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, "timeInMillis", "repetitionTimeInMillis", pendingintent);

This guarantees that before setting an alarm will cancel all previously alarm with the same PendingIntent.

Hope you got this!

like image 95
imthegiga Avatar answered Sep 21 '22 13:09

imthegiga


I think you could get an eye on : AlarmManager.Cancel

And on that Question/Answer: Android: Get all PendingIntents set with AlarmManager

As stated in there you can't ask to the AlarmManager to tell you what PendingIntent are in it. But I think you can make some PendingIntent similar to the one you want to cancel ;).

like image 34
BigbangRevo Avatar answered Sep 22 '22 13:09

BigbangRevo