title: Cannot bind to service in android
body: <p>I am working on a larger project but have become stuck on a very simple element that is binding to an android service. My issue is that I cannot seem to bind to the service. No errors are being thrown at all when calling <code>bindService()</code> and I have used the internal debugger and manual tracing of print statements and I am still failing to understand what is causing this error. Any help appreciated! (This is especially irritating as it is largely C+P from the android development website yet still won't work!)</p>
  
  <p>Main Activity:</p>
  
  <pre><code>package com.example.servicetest;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  
  public class MainActivity extends Activity {
  
  LocalService mService;
  boolean mBound = false;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // START Activity
      Intent intent = new Intent(this, BindingActivity.class);
      startActivity(intent);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  }
  </code></pre>
  
  <p>Binding Activity:</p>
  
  <pre><code>package com.example.servicetest;
  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.content.ServiceConnection;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.view.View;
  import android.widget.Toast;
  
  import com.example.servicetest.LocalService.LocalBinder;
  
  public class BindingActivity extends Activity {
      LocalService mService;
      boolean mBound = false;
  
      /** Defines callbacks for service binding, passed to bindService() */
      private ServiceConnection mConnection = new ServiceConnection() {
  
          @Override
          public void onServiceConnected(ComponentName className,
                  IBinder service) {
              // We've bound to LocalService, cast the IBinder and get LocalService instance
              LocalBinder binder = (LocalBinder) service;
              mService = binder.getService();
              mBound = true;
              System.out.println("End onServiceConnected");
          }
  
          @Override
          public void onServiceDisconnected(ComponentName arg0) {
              mBound = false;
          }
      };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_bind);
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          // Bind to LocalService
          System.out.println("onStart called");
          Intent intent = new Intent(this, LocalService.class);
          bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
          this.startService(intent);
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          // Unbind from the service
          if (mBound) {
              unbindService(mConnection);
              mBound = false;
          }
      }
  
      /** Called when a button is clicked (the button in the layout file attaches to
        * this method with the android:onClick attribute) */
      public void onButtonClick(View v) {
  
          if (mBound) {
              // Call a method from the LocalService.
              // However, if this call were something that might hang, then this request should
              // occur in a separate thread to avoid slowing down the activity performance.
               System.out.println("Button click");
              int num = mService.getRandomNumber();
              Toast.makeText(this, "number: " + num, Toast.LENGTH_SHORT).show();
              System.out.println("" + num);
          } else {
               System.out.println("Not bound");
          }
      }
  
  
  }
  </code></pre>
  
  <p>LocalService:</p>
  
  <pre><code>package com.example.servicetest;
  
  import java.util.Random;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.Binder;
  import android.os.IBinder;
  
  public class LocalService extends Service {
      // Binder given to clients
      private final IBinder mBinder = new LocalBinder();
      // Random number generator
      private final Random mGenerator = new Random();
  
      /**
       * Class used for the client Binder.  Because we know this service always
       * runs in the same process as its clients, we don't need to deal with IPC.
       */
      public class LocalBinder extends Binder {
          LocalService getService() {
              // Return this instance of LocalService so clients can call public methods
              return LocalService.this;
          }
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          System.out.println("onBind called");
          return mBinder;
      }
  
      /** method for clients */
      public int getRandomNumber() {
        return mGenerator.nextInt(100);
      }
  
  }
  </code></pre>
  
  <p>And finally the manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.servicetest"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.servicetest.MainActivity"
              android:label="@string/app_name" &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;activity
              android:name="com.example.servicetest.BindingActivity"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>