title: AlarmManager.setRepeating - Service does not seem to repeat
body: <p>I'm working on an Android app that sues a background task to fetch XML from a URL every x seconds (user defined interval, defaulting to 60). My structure is as so:</p>
  
  <p><strong>MainActivity</strong></p>
  
  <p>This schedules an Alarm via AlarmManager:</p>
  
  <pre><code>public static void scheduleAlarm(Context voContext, int viCheckInterval)
  {
      try {
          moAlarmManager = (AlarmManager) voContext.getSystemService(Context.ALARM_SERVICE);
          Intent intent = new Intent(voContext, AlarmReceiver.class);
          moAlarmIntent = PendingIntent.getBroadcast(voContext, 0, intent,
                                                      PendingIntent.FLAG_CANCEL_CURRENT);
          Calendar time = Calendar.getInstance();
          time.setTimeInMillis(System.currentTimeMillis());
          time.add(Calendar.SECOND, viCheckInterval);
          moAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
                                         time.getTimeInMillis(), moAlarmIntent);
      } catch (Exception e) {
          Log.e("MessageCheckAlarmHandler", e.toString());
      }
  }
  </code></pre>
  
  <p><strong>AlarmReceiver</strong></p>
  
  <p>This is a BroadcastReceiver that starts the service:</p>
  
  <pre><code>@Override
  public void onReceive(Context context, Intent intent)
  {
      Context oAppContext = context.getApplicationContext();
  
      if (oAppContext == null) {
          oAppContext = context;
      }
  
      Intent serviceIntent = new Intent(oAppContext, MessagingService.class);
      oAppContext.startService(serviceIntent);
  }
  </code></pre>
  
  <p><strong>MessagingService</strong></p>
  
  <p>This creates our in-house logger (logging over TCP) and starts an AsyncTask called FetchPageTask:</p>
  
  <pre><code>public class MessagingService extends Service
  {
      @Override
      public void onCreate()
      {
          super.onCreate();
  
          ...
  
          this.acquireLocks();
  
          try {
              String sCheckerUrl = oPreferences.getString("pref_server", "");
              int sCheckerPort = Integer.parseInt(oPreferences.getString("pref_server_port",
                                                                             "8050"));
              sCheckerUrl = String.format(URL, sCheckerUrl, sCheckerPort);
  
              this.moFetchInboxTask = new FetchPageTask(this.logger, this);
              this.moFetchInboxTask.execute(sCheckerUrl);
          } finally {
              this.releaseLocks();
              this.stopSelf();
          }
      }
  
      @Override
      public void onDestroy()
      {
          super.onDestroy();
  
          this.logger.close();
      }
  
      @Override
      public IBinder onBind(Intent intent)
      {
          return null;
      }
  
      /**
       * Acquire a WakeLock and a WifiLock.
       */
      private void acquireLocks()
      {
          try {
              // Acquire a wake lock to prevent the device from entering "deep sleep"
              PowerManager oPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
              this.moWakeLock = oPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
              this.moWakeLock.acquire();
              // Acquire a WiFi lock to ensure WiFi is enabled
              WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
              this.moWifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
              this.moWifiLock.acquire();
          } catch (Exception e) {
              this.logger.error(TAG + "-&gt;onCreate()", "Error acquiring locks: " + e.toString());
          }
      }
  
      /**
       * Release our WakeLock and WifiLock.
       */
      private void releaseLocks()
      {
          try {
              this.moWakeLock.release();
              this.moWifiLock.release();
          } catch (Exception e) {
              this.logger.error(TAG + "-&gt;releaseLocks()", e.toString());
          }
      }
  }
  </code></pre>
  
  <p><strong>FetchPageTask</strong></p>
  
  <p>This extends AsyncTask and does all the work of fetching the page and parsing the XML. It also then adds notifications and performs actions on the data retreived if need be.</p>
  
  <hr>
  
  <p>This all works fine once, but does not run subsequently. I know that the AsyncTask works as I was previously doing it using pure Java via ScheduledExecutorService and ScheduledFuture and it worked. The only reason I decided to change to using AlarmManager is for maintainability purposes.</p>