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.
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.
What condition can I test for at the point marked ‘<<<<<‘ in the code below to skip calling unbind again?
My activity code is
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) // <<<< 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");
}
};
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 :
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
I note that the activity’s onServiceDisconnected() never gets called, is this normal?
The simplest thing to do would be to introduce another variable, say,
isServConnBound, and add checks on both bind and unbind actions. Of course, remember to update the variable after you callbindServiceandunbindService.