A problem is suddenly encountered, the problem is described:
I bind a service in order: Start->bind finally unbind when exiting the activity, and now I have the business need to bind again when I enter the activity again, and I find that calling the Bind method does not bind the service (I don't know why Google engineers are designing this.) )
Write a test code to verify:
Service:
package org.load.testservice;import android.app.service;import android.content.intent;import android.os.ibinder;public class myservice extends service {@Overridepublic ibinder onbind (intent intent) { System.out.println ("bind ...."); return null;} @Overridepublic void oncreate () {system.out.println ("OnCreate ..."); Super.oncreate (); @Overridepublic int onstartcommand (Intent intent, int flags, int startid) {system.out.println ("OnStart ..."); Return super.onstartcommand (Intent, flags, startid);} @Overridepublic boolean onunbind (intent intent) {system.out.println ("Unbind ..."); return super.onunbind (intent);} @Overridepublic void ondestroy () {system.out.println ("Destroy ..."); Super.ondestroy ();}}
Activity
package org.load.testservice;import android.app.activity;import android.content.componentname; import android.content.context;import android.content.intent;import android.content.serviceconnection;import android.os.bundle;import android.os.ibinder;import Android.view.view;public class mainactivity extends activity {private myconn conn = new myconn (); @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} Public void startservice (View view) {startservice (New intent (this, Myservice.class));} Public void bindservice (View view) {bindservice (New intent (this, MyService.class ), conn, context.bind_auto_create);} Public void unbindservice (View view) {unbindservice (conn);} private class myconn implements  serviceconnection {@Overridepublic void onserviceconnected (componentname name, Ibinder service) {} @Overridepublic void onservicedisconnected (componentname name) {}}}
The first time you click the button in order to find that there is not much problem, the service in turn create, start, bind, unbind.
But when I click "Bind service" again, the problem appears Onbind method did not execute!! In other words, no successful binding!!!
View the document see the service there is also a life cycle method Onrebind, as if seeing hope, add up and try again:
@Overridepublic void Onrebind (Intent Intent) {System.out.println ("rebind ..."); Super.onrebind (Intent);}
With excitement again test, logcat regret tell me or not!!!!
Take a closer look at Onrebind's official notes:
Called when new clients has connected to the service, after it had previously been notified-all had disconnected in Its onUnbind .
This would only be called if the implementation onUnbind of is overridden to return true.
The most important is the last word, meaning: This method can only be called when Onunbind returns True.
Now rewrite Onunbind:
@Overridepublic boolean onunbind (Intent Intent) {System.out.println ("unbind ..."); return true;
Look at Logcat again, not much else said:
Summarize:
Multiple bind, unbind a service, only the first time to call the service's Onbind method, unbind and continue to bind and will not call Onbind instead of calling Onrebind, But one more condition for onrebind to make a smooth call is that Onunbind must return true.