title: Why does my Android service get restarted when the process is killed, even though I used START_NOT_STICKY?
body: <p>My app uses a pattern where I start a service with <a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" rel="noreferrer">Context#startService()</a> as well as bind to it with <a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" rel="noreferrer">Context#bindService()</a>.  This is so that I can control the lifetime of the service independently from whether any clients are currently bound to it.  However, I noticed recently that whenever my app is killed by the system, it soon restarts any services that were running.  At this point the service will never be told to stop, and this is causing battery drain whenever it happens.  Here's a minimal example:</p>
  
  <p>I found someone with a similar issue <a href="https://groups.google.com/forum/?fromgroups=#!topic/android-developers/x4pYZcXeKsw" rel="noreferrer">here</a>, but it wasn't ever diagnosed or solved.</p>
  
  <p>Service:</p>
  
  <pre><code>@Override
  public void onCreate() {
      Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show();
  }
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      return START_NOT_STICKY;
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return new Binder();
  }
  </code></pre>
  
  <p>Activity:</p>
  
  <pre><code>@Override
  protected void onStart() {
      super.onStart();
      Intent service = new Intent(this, BoundService.class);
      startService(service);
      bindService(service, mServiceConnection, 0);
  }
  
  @Override
  protected void onStop() {
      unbindService(mServiceConnection);
      Toast.makeText(this, "unbindService", Toast.LENGTH_SHORT).show();
      super.onStop();
  }
  </code></pre>
  
  <p>To test it, I launched the app, which started the service and bound to it.  Then I backed out of the app, which unbinds (but leaves the service running).  Then I did</p>
  
  <pre><code>$ adb shell am kill com.tavianator.servicerestart
  </code></pre>
  
  <p>and sure enough, 5 seconds later, the "onCreate" toast appears, indicating that the service started again.  Logcat shows this:</p>
  
  <pre><code>$ adb logcat | grep BoundService
  W/ActivityManager(  306): Scheduling restart of crashed service com.tavianator.servicerestart/.BoundService in 5000ms
  I/ActivityManager(  306): Start proc com.tavianator.servicerestart for service com.tavianator.servicerestart/.BoundService: pid=20900 uid=10096 gids={1028}
  </code></pre>
  
  <p>If I replace the startService() pattern with BIND_AUTO_CREATE, the problem doesn't occur (even if I crash the app while it's still bound to the service).  It also works if I never bind to the service.  But the combination of start, bind, and unbind seems to never let my service die.</p>
  
  <p>Using dumpsys before killing the app shows this:</p>
  
  <pre><code>$ adb shell dumpsys activity services com.tavianator.servicerestart
  ACTIVITY MANAGER SERVICES (dumpsys activity services)
    Active services:
    * ServiceRecord{43099410 com.tavianator.servicerestart/.BoundService}
      intent={cmp=com.tavianator.servicerestart/.BoundService}
      packageName=com.tavianator.servicerestart
      processName=com.tavianator.servicerestart
      baseDir=/data/app/com.tavianator.servicerestart-2.apk
      dataDir=/data/data/com.tavianator.servicerestart
      app=ProcessRecord{424fb5c8 20473:com.tavianator.servicerestart/u0a96}
      createTime=-20s825ms lastActivity=-20s825ms
      executingStart=-5s0ms restartTime=-20s825ms
      startRequested=true stopIfKilled=true callStart=true lastStartId=1
      Bindings:
      * IntentBindRecord{42e5e7c0}:
        intent={cmp=com.tavianator.servicerestart/.BoundService}
        binder=android.os.BinderProxy@42aee778
        requested=true received=true hasBound=false doRebind=false
  </code></pre>