Use mIsBound inside doBindService() and doUnbindService() instead of in the ServiceConnection instance.
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = (MyIBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBinder = null;
}
};
...
public void doBindService() {
mIsBound =bindService(new Intent(this, MyService.class),
mConnection, Context.BIND_AUTO_CREATE);
}
public void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
This is how it’s done in http://developer.android.com/reference/android/app/Service.html