title: Should onHandleIntent be called when IntentService is started with bindService?
body: <p>My service extends <code>IntentService</code> and when it is started with <code>startService</code>, <code>onHandleIntent</code> gets called. However, when the service is started with <code>bindService</code> (I do need binding), <code>onHandleIntent</code> does not get called.</p>
  
  <p>Should <code>onHandleIntent</code> be called when <code>IntentService</code> is started with <code>bindService</code>? Is <code>startService</code> the only way <code>IntentService</code> should be started?</p>
  
  <p>The documentation for <code>IntentService</code> says the following:</p>
  
  <p><em><strong>Clients send requests through startService(Intent) calls;</strong> the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.</em></p>
  
  <p>Currently I solve my problem by calling <code>startService</code> right after <code>bindService</code> but I find it ugly. I would like to know is there a way to make it work with just one call.</p>
  
  <p>Code snippets follow, it might be that I am missing something obvious.</p>
  
  <p><strong>ExampleService.java</strong></p>
  
  <pre><code>public class ExampleService extends IntentService {
  
  private class IncomingHandler extends Handler {
  
      @Override
      public void handleMessage(Message message) {
  
          if (message.replyTo != null) {
              outMessenger = message.replyTo;
          }
      }
  }
  
  private Messenger messenger = new Messenger(new IncomingHandler()); 
  private Messenger outMessenger = null;
  
  
  public ExampleService() {
      super("ExampleService");
  }
  
  @Override
  public IBinder onBind(Intent intent) {
      return messenger.getBinder();
  }
  
  @Override
  protected void onHandleIntent(Intent arg0) {
  
      System.out.println("Service started");
  
      for (int i = 0; i &lt; 5; i++) {
  
          SystemClock.sleep(5000);
  
          if (outMessenger != null) {
              try {
                  outMessenger.send(Message.obtain());
              } catch (RemoteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  }
  </code></pre>
  
  <p><strong>Service Manifest.xml</strong></p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.service"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
      android:minSdkVersion="3"
      android:targetSdkVersion="15" /&gt;
  
  &lt;application
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;service android:name=".ExampleService"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.example.service.ExampleService" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong> (caller)</p>
  
  <pre><code>public class MainActivity extends Activity implements ServiceConnection {
  
  
  private class IncomingHandler extends Handler {
  
      @Override
      public void handleMessage(Message msg) {
  
          Toast.makeText(getApplicationContext(), "Message received", Toast.LENGTH_SHORT).show();
          System.out.println("Message received!");
          super.handleMessage(msg);
      }
  }
  
  private Messenger messenger = new Messenger(new IncomingHandler());
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Button button = (Button) findViewById(R.id.button1);
      button.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              Intent intent = new Intent("com.example.service.ExampleService");
              bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE);
              //startService(intent);
          }
      });
  
  }
  
  @Override
  public void onServiceConnected(ComponentName name, IBinder binder) {
  
      Message message = Message.obtain();
      message.replyTo = messenger;
  
      try {
          new Messenger(binder).send(message);
      } catch (RemoteException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  @Override
  public void onServiceDisconnected(ComponentName name) {
      // TODO Auto-generated method stub
  }
  }
  </code></pre>