title: bound service versus started service on Android and how to do both
body: <p>I am asking a vexed question that has been (partially, to my mind) addressed <a href="https://stackoverflow.com/questions/5243562/lifecycle-of-service-that-is-started-and-bound">here</a> and <a href="https://stackoverflow.com/questions/3514287/android-service-startservice-and-bindservice">here</a>. Let's say like in many examples we want to create a music application, using (say) a single activity and a service. We want the service to persist when the Activity is stopped or destroyed. This kind of lifecycle suggests a started service:</p>
  
  <blockquote>
    <p>A service is "started" when an application component (such as an
    activity) starts it by calling startService(). Once started, a service
    can run in the background indefinitely, even if the component that
    started it is destroyed</p>
  </blockquote>
  
  <p>Ok, but we also want to be able to communicate with the service, so we need a service binding. No problem, we have both a bound and started service as <a href="https://stackoverflow.com/questions/3514287/android-service-startservice-and-bindservice">this answer suggests</a>:</p>
  
  <ul>
  <li>At activity startup (or some other point) we call <a href="http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29" rel="nofollow noreferrer">startService()</a></li>
  <li>After that we call <a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" rel="nofollow noreferrer">bindService()</a> to obtain the <a href="http://developer.android.com/reference/android/os/IBinder.html" rel="nofollow noreferrer">IBinder</a> interface, and proceed from there.</li>
  </ul>
  
  <p>So far so good, but a problem arises from the fact that when the activity starts, we do not know if the service is around or not. It may have been started or it may not have been. The answer could be something like:</p>
  
  <ul>
  <li>At startup, try to bind to the service (use <a href="http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29" rel="nofollow noreferrer">bindService()</a> without the <a href="http://developer.android.com/reference/android/content/Context.html#BIND_AUTO_CREATE" rel="nofollow noreferrer">BIND_AUTO_CREATE</a> flag)</li>
  <li>If that fails, then start the service using <code>startService()</code>, and then bind to it.</li>
  </ul>
  
  <p>This idea is premised on a particular reading of the docs for <code>bindService()</code>:</p>
  
  <blockquote>
    <p>Connect to an application service, creating it if needed. </p>
  </blockquote>
  
  <p>If zero flag means "service is not really needed" than we are OK. So we try something like this using the following code:</p>
  
  <pre><code>private void connectToService() {
      Log.d("MainActivity", "Connecting to service");
      // We try to bind to an existing service
      Intent bindIntent = new Intent(this, AccelerometerLoggerService.class);
      boolean bindResult = bindService(bindIntent, mConnection, 0);
      if (bindResult) {
          // Service existed, so we just bound to it
          Log.d("MainActivity", "Found a pre-existing service and bound to it");
      } else {
          Log.d("MainActivity", "No pre-existing service starting one");
          // Service did not exist so we must start it
  
          Intent startIntent = new Intent(this, AccelerometerLoggerService.class);
          ComponentName startResult = startService(startIntent);
          if (startResult==null) {
              Log.e("MainActivity", "Unable to start our service");
          } else {
              Log.d("MainActivity", "Started a service will bind");
              // Now that the service is started, we can bind to it
              bindService(bindIntent, mConnection, 0);
              if (!bindResult) {
                  Log.e("MainActivity", "started a service and then failed to bind to it");
              } else {
                  Log.d("MainActivity", "Successfully bound");
              }
          }
      }
  }
  </code></pre>
  
  <p>And what we get is a successful binding every time:</p>
  
  <pre><code>04-23 05:42:59.125: D/MainActivity(842): Connecting to service
  04-23 05:42:59.125: D/MainActivity(842): Found a pre-existing service and bound to it
  04-23 05:42:59.134: D/MainActivity(842): onCreate
  </code></pre>
  
  <p>The global question is "Am I misunderstanding bound versus started services and how to use them?" More specific questions are:</p>
  
  <ul>
  <li>Is it the correct understanding of the docs to think that zero flag passed to <code>bindService()</code> means "Do not start the service"?  If not, is there no way to call <code>bindService()</code> without starting the service?</li>
  <li>Why does <code>bindService()</code> return <code>true</code> even if the service is not running? In this case it doesn't seem like the service has been started, based on <code>Log</code> calls.</li>
  <li>If there the previous point is the correct/expected behavior of <code>bindService()</code>, is there a workaround (i.e. somehow ensure that <code>startService</code> is called only if the service is not running?)</li>
  </ul>
  
  <p>P.S. I've moved on from the problem in my own code: I issue <code>startService()</code> calls regardless, since repeated <code>startService()</code> are simply ignored. However, I would still like to understand the issues better.</p>