Android: How to safely unbind a service
<p>I have a service which is binded to application context like this: 
<code>getApplicationContext().bindService(
                    new Intent(this, ServiceUI.class),
                    serviceConnection,
                    Context.BIND_AUTO_CREATE
            );</code></p>

<p><code>protected void onDestroy() {
            super.onDestroy();<br>
            getApplicationContext().unbindService(serviceConnection);
        }</code></p>

<p>For some reason, only sometimes the application context does not bind properly (I can't fix that part), however in onDestroy() I do <code>unbindservice</code> which throws an error</p>

<pre><code>java.lang.IllegalArgumentException: Service not registered: tools.cdevice.Devices$mainServiceConnection.
</code></pre>

<p>My question is: Is there a way to call <code>unbindservice</code> safely or check if it is already bound to a service before unbinding it?</p>

<p>Thanks in advance.</p>
