title: AlarmManager, BroadcastReceiver and Service not working
body: <p>I'm refactoring some code so that my app will pull data from a website once a day at a given time. From my research, it seems like <code>AlarmManager</code> is the most appropriate approach.</p>
  
  <p>The tutorial I have been following is: <a href="http://mobile.tutsplus.com/tutorials/android/android-fundamentals-downloading-data-with-services/">http://mobile.tutsplus.com/tutorials/android/android-fundamentals-downloading-data-with-services/</a></p>
  
  <p>So far, <code>AlarmManager</code> and the <code>BroadcastReceiver</code> seem to be working, however the <code>Service</code> never seems to start (ie. <code>onStartCommand</code> doesn't seem to be called)</p>
  
  <p>Here are the important snippets of the code I have so far:</p>
  
  <p><strong>MyActivity.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>private void setRecurringAlarm(Context context) {
      Calendar updateTime = Calendar.getInstance();
      updateTime.setTimeZone(TimeZone.getDefault());
      updateTime.set(Calendar.HOUR_OF_DAY, 20);
      updateTime.set(Calendar.MINUTE, 30);
  
      Intent downloader = new Intent(context, AlarmReceiver.class);
      downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
      AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      // should be AlarmManager.INTERVAL_DAY (but changed to 15min for testing)
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent); 
      Log.d("MyActivity", "Set alarmManager.setRepeating to: " + updateTime.getTime().toLocaleString());
  }
  </code></pre>
  
  <p><strong>AlarmReceiver.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class AlarmReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent dailyUpdater = new Intent(context, MyService.class);
          context.startService(dailyUpdater);
          Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
      }
  }
  </code></pre>
  
  <p><strong>MyService.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyService extends Service {
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          Log.d("MyService", "About to execute MyTask");
          new MyTask().execute();
          return Service.START_FLAG_REDELIVERY;
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      private class MyTask extends AsyncTask&lt;String, Void, boolean&gt; {
          @Override
          protected boolean doInBackground(String... strings) {
              Log.d("MyService - MyTask", "Calling doInBackground within MyTask");
              return false;
          }
      }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;application ...&gt;
      ...
      &lt;service android:name="MyService"&gt;&lt;/service&gt;
      &lt;receiver android:name="AlarmReceiver"&gt;&lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>When I trigger the <code>setRecurringAlarm</code> in <code>MyActivity</code> the log prints as expected, similarly, every 15min the log from <code>AlarmReceiver</code> appears. However, I never see the log from <code>MyService</code> :(</p>
  
  <p>Example of what I see in the logs:</p>
  
  <pre><code>DEBUG/MyActivity(688): Set alarmManager.setRepeating to: Jan 29, 2012 8:30:06 PM
  DEBUG/AlarmReceiver(688): Called context.startService from AlarmReceiver.onReceive
  DEBUG/AlarmReceiver(688): Called context.startService from AlarmReceiver.onReceive
  </code></pre>
  
  <p>Can't seem to figure out what I've done wrong - my understanding from the <a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29">Android Dev Docs</a> is that in <code>AlarmReceiver</code> when I call <code>context.startService(dailyUpdater)</code> that should in turn call <code>onStartCommand</code> in <code>MyService</code>, though that doesn't seem to be the case!</p>
  
  <p>What am I doing wrong that is causing <code>MyService</code> to not start at all?</p>