I've a bound service that runs in separate process
public class DownloadService extends Service {
private Looper mLooper;
private ServiceHandler mServiceHandler;
private Messenger messenger;
private Messenger uiMessenger;
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_FOREGROUND);
thread.start();
mLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mLooper);
messenger = new Messenger(mServiceHandler);
}
private class ServiceHandler extends Handler {
public ServiceHandler() {
}
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_DOWNLOAD_REQUEST:
String url = msg.getData().getString("url");
Log.d("test", url);
// download(url);
break;
default:
super.handleMessage(msg);
break;
}
}
}
@Override
public IBinder onBind(Intent intent) {
Log.d("test", "onBind");
return messenger.getBinder();
}
@Override
public void onRebind(Intent intent) {
Log.d("test", "onRebind");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("test", "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.d("test", "onDestroy");
super.onDestroy();
}
}
And also a ServiceConnection like this:
serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
serviceMessenger = null;
isConnected = false;
Log.d("test", "disconn");
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
if (serviceMessenger == null) {
serviceMessenger = new Messenger(service);
}
isConnected = true;
Log.d("test", "conn");
send();
}
};
Request for bind to service
Intent intent = new Intent(context, DownloadService.class);
context.bindService(intent, serviceConnection,
Context.BIND_AUTO_CREATE);
And unBind request
context.unbindService(serviceConnection);
Once a call bindService every thing is good and work properly and onServiceConnected
method of ServiceConnection
invoked, but when I call unBind
method onServiceDisconnected
is not called! while onUnbind
and onDestroy
methods of service are invoked.
And more surprisingly, I can communicate with service using serviceMessenger
that made in previous bind
!
Summary
1. Why onServiceDisconnected
not invoked when I call unBind
method?
2- Why it is possible to communicate with a service that destroyed due to unBind
call?