title: Android Service has leaked even though it is (supposedly) not running
body: <p>in <code>onDestroy()</code> I check whether the service is still running using the code below. If it is - I unbind and stop it.</p>
  
  <pre><code>public boolean isServiceRunning(Class&lt;?&gt; serviceClass) {
          String serviceClassName = serviceClass.getName();
          final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
          final List&lt;RunningServiceInfo&gt; services = activityManager.getRunningServices(Integer.MAX_VALUE);
  
          for(RunningServiceInfo runningServiceInfo : services){
              if(runningServiceInfo.service.getClassName().equals(serviceClassName)){
                  return true;
              }
          }
          return false;
      }
  </code></pre>
  
  <p>Now I have a situation when <code>isServiceRunning</code> returns false, but after <code>onDestroy()</code> I get an error saying that ServiceConnection has leaked. Why would that be?</p>
  
  <p><strong>Edit</strong>:</p>
  
  <p>That's how I start the <code>Service</code> (in <code>onCreate()</code>):</p>
  
  <pre><code>startService(posServiceIntent);
  bindService(posServiceIntent, posConn, BIND_AUTO_CREATE);
  </code></pre>
  
  <p>and</p>
  
  <pre><code>posServiceIntent = new Intent(getApplicationContext(), PositionService.class);
  
  private ServiceConnection posConn = new PosServiceConnection();
  public class PosServiceConnection implements ServiceConnection {
          @Override
          public void onServiceConnected(ComponentName name, IBinder service) {
              Log.d(TAG, "PosServiceBinder connected [name: " + name.toShortString() + "].");
          }
          @Override
          public void onServiceDisconnected(ComponentName name) {
              Log.d(TAG, "PosServiceBinder disconnected [name: " + name.toShortString() + "].");
          }
      }
  
  protected void onDestroy() {
          if(isServiceRunning(PositionService.class)){
              Log.d(TAG, "Stopping PositionService in " + MainActivity.class.getSimpleName() + ".onDestroy()");
              unbindService(posConn);
              stopService(posServiceIntent);
          }
  </code></pre>