android - unBind a bound service -
i've bound service 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 serviceconnection 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 bind service
intent intent = new intent(context, downloadservice.class); context.bindservice(intent, serviceconnection, context.bind_auto_create);
and unbind request
context.unbindservice(serviceconnection);
once call bindservice every thing , work , onserviceconnected
method of serviceconnection
invoked, when call unbind
method onservicedisconnected
not called! while onunbind
, ondestroy
methods of service invoked.
and more surprisingly, can communicate service using servicemessenger
made in previous bind
!
summary
1. why onservicedisconnected
not invoked when call unbind
method?
2- why possible communicate service destroyed due unbind
call?
Comments
Post a Comment