title: Service started by AlarmManager gets restarted with null intent after application removed from apps tray
body: <p>I'm beginner android programmer. While experimenting with background Services and background data download I came across this particular problem:</p>
  
  <p>I'm using an AlarmManager to schedule repeating background data download.</p>
  
  <pre><code>public void setBackgroundDataService(Context context, long time_in_millis) {
  
      Intent background_service_intent = new Intent(context, BackgroundDataService.class);
      background_service_intent.putExtra("value", val);
      PendingIntent pending_intent = PendingIntent.getService(context, BACKGROUND_DATA_SERVICE_ID , background_service_intent, PendingIntent.FLAG_UPDATE_CURRENT);
  
      cancelBackgroundServiceAlarmIfExists(context, background_service_intent);
  
      AlarmManager alarm_manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
      alarm_manager.setRepeating(AlarmManager.RTC, (System.currentTimeMillis() + time_in_millis), time_in_millis, pending_intent);
  }
  
  
  public static void cancelBackgroundServiceAlarmIfExists(Context context, Intent intent) {
  
      // try to cancel Pending Intent if exists
      try {
          PendingIntent pending_intent = PendingIntent.getService(context, BACKGROUND_DATA_SERVICE_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          AlarmManager alarm_manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
          alarm_manager.cancel(pending_intent);
          Log.d("DEBUG", "Background service terminated");
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Here is onStartCommand() from Service:</p>
  
  <pre><code>public int onStartCommand(Intent intent, int flags, int startId) {
  
      Log.d("DEBUG", "Background service invoked");
  
      if (intent == null)
          return super.onStartCommand(intent, flags, startId);
  
      Bundle extras = intent.getExtras();
  
      some_value = extras.getString("value");
  
      startDataDownload();
  
      return super.onStartCommand(intent, flags, startId);
  }
  </code></pre>
  
  <p>Data download is performed in AsyncTask</p>
  
  <p>So the problem is:
  Why after swiping the app from applications tray Service gets restarted and intent passed in arguments is null. Proper working of the Service relies on extras passed in the intent. At first my application would just crash (usually 2 times, I assume that something was trying to restart it, but crashed while trying to get extras from null intent). After some time Service would start properly and do its job without crashing (probably AlarmManager restarted it with proper intent extras after specified time). Then sporadically it would crash again. I managed to avoid crashes by checking whether intent is null or not. It seems to work. But the question remains. </p>
  
  <blockquote>
    <p>Why is my Service getting restarted and null intent gets passed to it right after I swipe my app from application tray. Is there anything thet can be done about it? Is there a better way to download data periodically?</p>
  </blockquote>