Defensive code for controlling a service
<p>I'm trying to revamp my multi-activity app to use just once instance of a LocationListener which I intend to implement in a service. Prior to doing this I've been experimenting with a stub activity and a stub service to see what happens under error conditions.</p>

<p>I want to see what happens if I attempt to unbind from a service which has already been unbound and avoid any errors if this should happen. The activity has two buttons to bind/unbind. If I deliberately hit the unbind twice in succession I do get a runtime error.</p>

<p><strong>What condition can I test for at the point marked '&lt;&lt;&lt;&lt;&lt;' in the code below to skip calling unbind again?</strong></p>

<p>My activity code is</p>

<pre><code>public void myClickHandler(View target) {
    switch (target.getId()) {
    case R.id.bind:
        Log.d("STAG", "Activity One pressed BIND button");
        mServiceConnected = bindService(new Intent(
                "com.nbt.servicetest.LOCATIONSERVICE"), mServconn,
                Context.BIND_AUTO_CREATE);
        break;
    case R.id.unbind:
        Log.d("STAG", "Activity One pressed UNBIND button");
        try{
        if (mServconn != null) // &lt;&lt;&lt;&lt; What to put here if already unbound?
            unbindService(mServconn);}
        catch(Exception e){
            Log.d("STAG", "Exception " + e.getMessage());
        }
        break;
    }
}

ServiceConnection mServconn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d("STAG", "Activity One service connected");
        mIbinder = service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.d("STAG", "Activity One service disconnected");
    }
};
</code></pre>

<p>The service is starting/stopping OK. I've put log lines in the service code with the same tag on all the pertinent lines. The output is :</p>

<pre><code>STAG(2945): Activity One onCreate
STAG(2945): Activity One onStart
STAG(2945): Activity One onResume
STAG(2945): Activity One pressed BIND button
STAG(2945): Loc service ONCREATE
STAG(2945): Loc service ONBIND
STAG(2945): Activity One service connected
STAG(2945): Activity One pressed UNBIND button
STAG(2945): Loc service ONUNBIND
STAG(2945): Loc service ONDESTROY
STAG(2945): Activity One pressed UNBIND button
STAG(2945): Exception Service not registered: com.nbt.servicetest.ServiceTesterActivityOne$1@43b8b290
</code></pre>

<p>I note that the activity's <strong>onServiceDisconnected()</strong> never gets called, is this normal?</p>
