title: Understanding foreground service lifecycle
body: <p>I'm trying to implement a foreground service and I'm trying to understand how it handles crashes.</p>
  
  <p>I created a simple service like the one one below:</p>
  
  <pre><code>@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      checkRunningAndStartForeground();
      return super.onStartCommand(intent, flags, startId);
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      crashAfterTenSeconds();
  }
  
  @Override
  public void onDestroy {
      super.onDestroy();
      if(isRunning) {
          isRunning = false;
          stopForeground(true);
      }
  }
  </code></pre>
  
  <p>What happens is that the service starts in foreground, and then crashes, as expected. It is the scheduled for restart by the system. It restarts and then crashes again, as expected. After that, it is not scheduled for restart.</p>
  
  <p>My question is, why does it restart once, but not twice? I want it to keep restarting after each crash.</p>
  
  <p>What I am worried about is that if my application is running, and somehow crashes twice, then it will never be restarted. </p>
  
  <p>For example, it crashes once in the beginning, runs for a day or two and crashes again, does that mean it won't come back?</p>