title: Android - Avoid the alarm triggering when user changes date/time
body: <p>I have an app that sends periodic alarms every day of the week, through the method <code>setRepeating</code> from <code>AlarmManager</code>:</p>
  
  <pre><code>    Intent intent = new Intent(context, AlarmReceiver.class);
      intent.putExtra(INTENT_TAG_ALERT_ID, lastAlertId.getId()+"");
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
      AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cDate.getTimeInMillis(), ((AlarmManager.INTERVAL_DAY)*7), pendingIntent);
  </code></pre>
  
  <p>And then get the alarm on my <code>AlarmReceiver</code> defined on manifest as a Receiver:</p>
  
  <pre><code>    &lt;receiver
          android:name=".manager.alarm.AlarmReceiver"
          android:exported="true"&gt;
      &lt;/receiver&gt; 
  </code></pre>
  
  <p>The problem is when the user changes the Date/Time from his phone for a day or 2 in the future, I receive several calls from the receiver (depending on the number of days chosen). Since I use a <code>setRepeating</code> method I can't validate if the alarm was sent at the right time.</p>
  
  <p>I know this is normal procedure as described on here: 
  <a href="http://developer.android.com/training/scheduling/alarms.html" rel="nofollow">"A trigger time. If the trigger time you specify is in the past, the alarm triggers immediately."</a>, but I want to prevent it.</p>
  
  <p>I thought of putting a listener to know when the user changes the date and time of his phone, to unschedule all the alarms and then schedule them again, as you can see bellow:</p>
  
  <pre><code>public class DateTimeChangedBroadcastReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (action.equals(Intent.ACTION_TIMEZONE_CHANGED) || action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_DATE_CHANGED)) {
          AlarmScheduler.unscheduleAllAlarms();
          //and then
          AlarmScheduler.scheduleAllAlarms();
      }
  }
  }
  </code></pre>
  
  <p>And the manifest: </p>
  
  <pre><code>        &lt;receiver android:name=".manager.alarm.DateTimeChangedBroadcastReceiver"
          android:priority="1000"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.ACTION_TIMEZONE_CHANGED"/&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.ACTION_TIME"/&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.DATE_CHANGED" /&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.TIME_SET"/&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.ACTION_TIME_CHANGED"/&gt;
          &lt;/intent-filter&gt;
  
      &lt;/receiver&gt;
  </code></pre>
  
  <p>But in this solution, the receiver of time changes come after the receiver from the alarm, so it doesn't serve me much...</p>
  
  <p>Is there a way to choose the order of my receivers?</p>
  
  <p>Is there a better way to do this?</p>
  
  <p>I also though of putting a listener to clock time, to store it. if the user changes the hour I should only validate the difference... but I believe I can get a better way to do this (preferentially not involving another listener on the app)</p>