title: Android: AlarmManager on firing correct alarms
body: <p><strong>ALREADY ANSWERED, FOR THOSE WHO HAVE THE SAME PROBLEM PLEASE REFERE TO THE BOTTOM OF THIS POST</strong></p>
  
  <hr>
  
  <p>I have this part on my app which schedules an alarm, all was working well, it scheduled the alarm correctly but there is a erratic behavior wherein sometimes, when the alarm scheduled is fired, instead of firing once, it fired several times, i know this cuz everytime an alarm fires i displayed "ALARM FIRED" on logcat, there are several ALARM FIRED on my logcat, sometimes up to 10, what im confused about is that i only scheudled that alarm once, and it fired many times. Here is Codes: </p>
  
  <p><strong>THIS IS FOR SCHEDULING MY ALARM</strong> THIS IS ON THE <strong>Helper</strong> class accessed in static way</p>
  
  <pre><code>public final static String ACTION = "com.medical.organizer.utilities.NotifyReciever";
  private static NotifyReciever RECIEVER = new NotifyReciever();
  
  public static void scheduleAlarm(Context context,String message,int rCode, long triggerTime)
  {
      RECIEVER = new NotifyReciever();
      NotifyAlarm.TRIGGER_TIME = triggerTime;
      NotifyAlarm.CONTEXT = context;
  
      IntentFilter inFil = new IntentFilter(ACTION);
      NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
      Intent intent = new Intent(ACTION);
  
      intent.putExtra("message", message);
      NotifyAlarm.INTENT = intent;
      NotifyAlarm.startAlarm(rCode);
  }
  public static void cancelAlarm(Context context, int requestCode)
  {
          RECIEVER = new NotifyReciever();
          NotifyAlarm.CONTEXT = context;
  
          IntentFilter inFil = new IntentFilter(ACTION);
          NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
          Intent intent = new Intent(ACTION);
          NotifyAlarm.INTENT = intent;
          //context.unregisterReceiver(RECIEVER);
          NotifyAlarm.stopAlarm(requestCode);
  }
  </code></pre>
  
  <p><strong>THIS IS FOR THE ALARM ITSELF</strong></p>
  
  <pre><code>public class NotifyAlarm {
      public static Context CONTEXT;
      public static Intent INTENT;
      public static long TRIGGER_TIME;
  
  
      public static void startAlarm(int requestCode)
      {
          AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
          am.set(AlarmManager.RTC_WAKEUP, TRIGGER_TIME, pendingIntent);
          Log.d("AlarmCheck", "Alarm Started for the First Time, set() was called");
      }
      public static void stopAlarm(int requestCode)
      {
          AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
          am.cancel(pendingIntent);
          Log.d("ALARMSTOP","========ALARM WAS STOPPED AT THIS POINT==========");
      }
  }
  </code></pre>
  
  <p><strong>THIS IS FOR MY ALARM RECIEVER (BROADCAST RECIEVER)</strong></p>
  
  <pre><code>public class NotifyReciever extends BroadcastReceiver {
  private Context context;
      @Override
      public void onReceive(Context context, Intent intent) {
      this.context = context;
      Log.d("AlarmCheck","ALARM FIRED!");
          AlertDialog.Builder build = new AlertDialog.Builder(context);
          String message = intent.getStringExtra("message");
          build.setMessage(message);
              build.setCancelable(false);
          build.setPositiveButton("Snooze", new DialogInterface.OnClickListener() {
  
              public void onClick(DialogInterface dialog, int which) {
              //Rechedules the alarm after 25 secs time of interval is for
                      //testing purposes only 
                      NotifyReciever.this.context.unregisterReceiver(NotifyReciever.this);
                  Helper.scheduleAlarm(NotifyReciever.this.context, s, 
                    s.getRequestCode(),System.currentTimeMillis()+Helper.TWENTY_FIVE_SEC);
  
              }
          });
          build.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
  
              public void onClick(DialogInterface dialog, int which) {
                  //CANCELS THE ALARM     
                      Helper.cancelAlarm(NotifyReciever.this.context, s.getRequestCode());
              }
          });
  
          AlertDialog alert = build.create();
          alert.show();
  
      }
  }
  </code></pre>
  
  <p>Note that the receivers aren't declared through XML, they are registered programatically so to support dialogs. </p>
  
  <p>Consider this situation, this is where i notice that behavior always occurs, Say, i've added a schedule and scheduled the alarm @ 5:00am (Current time is 4:59), so at first it fired completely fine, then i decided to rescheduled that alarm to 5:01 (Current time 5:00) then this occurs, the alarm is fired for several times. the alear dialog appears several times, have to fast click them all to cancel the dialogs, sometimes my app crashes due to too many dialogs showing up. </p>
  
  <p>anyone knows how to go about this problem please do share and anybody knows how to properly schedule and fire an alarm please do share, as well as scheduling and firing multiple alarms as well as canceling them would be much appreciated. Explanation and some examples would be better.</p>
  
  <p>EDIT</p>
  
  <hr>
  
  <p><img src="https://i.stack.imgur.com/psaZL.png" alt="enter image description here"></p>
  
  <p>Legend: a colored dot on the side means that it is fired a at specific time and that was what logged on logcat, say an alarm fired @ 4:00 display the one with several blue dots</p>
  
  <p><strong>EDIT THIS IS THE ANSWER</strong></p>
  
  <hr>
  
  <p>So this is what i've discovered , mine is that you have to register your reciever only once, so to avoid multiple returns from the intentFilter, which matches all intents using ur ACTION, in my case i've registered my reciever over and over again so there are already many instances of intents with the same ACTIOn thus returning multiple recievers and yeah firing multiple alarms at a specified time instead of firing only one receiver, so yeah REGISTER OONLY ONCE YOUR RECEIVER MAYBE AT A GLOBAL CLASS IF YOU DECIDE NOT TO DECLARE YOUR RECEIVER IN XML </p>