title: Android: java.lang.IllegalArgumentException: Service not registered
body: <p>I have activity, that starts service and binds it. But I want my service to run even if the activity is closed, so I need to unbind it.
  When i am trying to do so (calling my function disconnectFromService), i always get this exception.</p>
  
  <p>Service is successfuly bound and I can communicate with it.
  I am trying to unbind the service only if is bound.</p>
  
  <p>I think, I try to bind and unbind the service from the same context - this - calling bind and unbind are from MainActivity functions.</p>
  
  <p>Main Activity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private boolean bounded;
  private SturkoPlayerService sturkoService;
  
  ...
  
  //Connection
  ...
  private ServiceConnection connection = new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
          SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service;
          sturkoService = mBinder.getService();
          bound = true;
          ...
          Log.d(DEBUG_S, "Service bind: " + (bounded &amp;&amp; sturkoService!=null)); //Binding successful           
      }
  
      @Override
      public void onServiceDisconnected(ComponentName name) {
          bound = false;
          sturkoService = null;
      }
  };
  ...
  
  
  //Binding in MainActivity code
  ...
  Intent startingIntent = new Intent(this, SturkoPlayerService.class);
  startingIntent.setAction(SturkoPlayerService.PLAYER_START);
  bindService(startingIntent, connection, Context.BIND_AUTO_CREATE);
  ...
  
  //Unbinding
  private void disconnectFromService() {
      if (sturkoService != null &amp;&amp; bound) {
          ...
          this.unbindService(connection);
      }
  }
  ...
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>public class SturkoPlayerService extends Service {
  ...
      public class LocalBinder extends Binder {
          public SturkoPlayerService getService() {
              return SturkoPlayerService.this;
          }
      }
  ...
  }
  </code></pre>
  
  <p><strong>EDIT1:</strong> Logcat</p>
  
  <pre><code>04-14 11:57:51.873 2890-2890 D/sturkoPlayer: START
  04-14 11:57:51.873 2890-2890 D/sturkoPlayer: Service bind: true
  04-14 11:58:01.406 2890-2890 W/System.err: java.lang.IllegalArgumentException: Service not registered: ....MainActivity$1@4221e2d8
  04-14 11:58:01.521 2890-2890 W/System.err:     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:947)
  04-14 11:58:01.521 2890-2890 W/System.err:     at android.app.ContextImpl.unbindService(ContextImpl.java:1277)
  04-14 11:58:01.522 2890-2890 W/System.err:     at android.content.ContextWrapper.unbindService(ContextWrapper.java:405)
  04-14 11:58:01.522 2890-2890 W/System.err:     at ...MainActivity.disconnectFromService(MainActivity.java:122)
  04-14 11:58:01.522 2890-2890 W/System.err:     at ...MainActivity.onStop(MainActivity.java:132)
  04-14 11:58:01.522 2890-2890 W/System.err:     at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1210)
  04-14 11:58:01.522 2890-2890 W/System.err:     at android.app.Activity.performStop(Activity.java:5158)
  04-14 11:58:01.523 2890-2890 W/System.err:     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3397)
  04-14 11:58:01.523 2890-2890 W/System.err:     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3456)
  04-14 11:58:01.525 2890-2890 W/System.err:     at android.app.ActivityThread.access$1200(ActivityThread.java:149)
  04-14 11:58:01.526 2890-2890 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
  04-14 11:58:01.527 2890-2890 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-14 11:58:01.527 2890-2890 W/System.err:     at android.os.Looper.loop(Looper.java:153)
  04-14 11:58:01.527 2890-2890 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4987)
  04-14 11:58:01.528 2890-2890 W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
  04-14 11:58:01.528 2890-2890 W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
  04-14 11:58:01.529 2890-2890 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
  04-14 11:58:01.531 2890-2890 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
  04-14 11:58:01.541 2890-2890 W/System.err:     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>Edit2:</strong> in manifest, under activity</p>
  
  <pre><code>&lt;service
      android:name=".Service.SturkoPlayerService"
      android:enabled="true"
      android:exported="true"&gt;&lt;/service&gt;
  </code></pre>
  
  <h2><strong>SOLUTION:</strong></h2>
  
  <p>Problem was, that "disconnectFromService()" function was called from finish() and from onStop() too. So it was called twice, when activity was about to close.</p>
  
  <p>After removing call from onStop(), the exception is thrown no more.</p>
  
  <p>Unfortunately this bug was just my stupidity.</p>