Android: Cancel an Alarm Set From Another Activity


I will try to explain this as best as I can. Basically, I have Activity 1 that uses an ExternalClass to do various things. Activity 2 also references Activity 1's object of said ExternalClass. From both of these activities I can set alarms using the AlarmManager, but I want to be able to cancel all the alarms created from either activity, from Activity 1.

All alarms are set using the same intent and the same AlarmManger (both created in the ExternalClass), but when I click my button in Activity 1 that is supposed to call myAlarms.cancel(intent) it only cancels the alarms that were created using the Activity 1 class.

The ExternalClass is referenced in Activity 2 by referencing the object of that class that was created in Activity 1 so they should both be using the same instance of the ExternalClass. I'm pretty sure it isn't canceling the alarms because of the context that was used when setting the alarms, but I can't figure out how to get around that.


To solve this issue I used to following code:

timerAlarmIntent = PendingIntent.getBroadcast(myContext, i, alarmIntent, 0); ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>(); intentArray.add(timerAlarmIntent); myAM.set(AlarmManager.RTC_WAKEUP, alarmTime, timerAlarmIntent);

I set the requestCode to a unique id. This is within a for loop and i represents 0, 1, 2...

To cancel the alarms I had to add each alarm to a list and loop through the list when I wanted to cancel all alarms.

private void cancelAlarms(){ if(intentArray.size()>0){ for(int i=0; i<intentArray.size(); i++){ myAM.cancel(intentArray.get(i)); } intentArray.clear(); }



To cancel and alarm you need to pass an equivalent PendingIntent (meaning p1.equals(p2) returns true) to the one used to create it. It doesn't matter from where you created the AlarmManager reference. How are you initializing the PendingIntent in both cases?

Two PendingIntents are considered equal if they both represent the same operation from the same package. Basically if you initialize two PendingIntents with equivalent Intents, they would be considered equal. EDIT: the documentation is obviously wrong about this, the requestCode is also used when comparing PendingIntents. See comments and other answer.


  • How to schedule and cancel tasks with asyncio
  • Cancelling a Windows service installation/Uninstallation with the Windows Installer causes service t
  • By reversing the polarity of a time domain signal, can I cancel the source audio?
  • Calling makeViewWithIdentifier:owner: causes ARC to re-create ivar
  • Aspect advising other aspects
  • How do I know what reference to include to import a specific .NET namespace?
  • Google Player Game Service Error: Can't build the sample game
  • Extracting individual digits from a float
  • Copying rows in a database when rows have children
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • cell spacing in div table
  • How can we prepend rows to a react native list-view?
  • C# - Most efficient way to iterate through multiple arrays/list
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • netsh acl setting (need alternative method - registry settings?)
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • How to revert to previous XCode version?
  • Remove final comma from string in vb.net
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Javascript Callbacks with Object constructor
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Build own AppleScript numerical error handling
  • ORA-29908: missing primary invocation for ancillary operator
  • Data Validation Drop Down Box Arrow Disappearing
  • python regex in pyparsing
  • Error creating VM instance in Google Compute Engine
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Java static initializers and reflection
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app