When to call Onrebind when binding a service

Source: Internet
Author: User

Serivce in Onrebind is called the timing is very special, want to know when Onrebind is called, you can take the following order to learn, finally naturally understand!

1. The first thing to know is that the same service can be either started or bound;

2. The Onrebind method in the service is called, as long as two prerequisites are met

(1) Onunbind method return value is true in the service

(2) The service object has not been destroyed after being unbound and then bound again

。 The following examples illustrate:

Example 1: The same Activity object

Start the service first (onCreate, Onstartcommand); re-bind service (onbind); Then unbind the service (Onunbind) (because the service is started, OnDestroy in the service will not be called); Rebind The service, this time the binding service object was created before, so this time the binding service will call the Onrebind method, and this time will not call the Onbind method.

Example 2: Not the same activity object

Open the project, start the mainactivity, start the service in activity (OnCreate, Onstartcommand), and then bind the service (onbind); And then unbind the service (Onunbind); The return key is then used to destroy the Mainactivity object (Onunbind), the project starts mainactivity again , and the service is bound, and the Onrebind method is called when the service is bound .

code example:

Activity_main.xml file

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height= "Match_parent" android:orientation= "vertical" tools:context= "com.qf.act.MainActivity" tools:ignore= "Mergerootfram E,orientation "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "a        ndroid:layout_height= "Wrap_content" android:text= "TextView1"/> <button android:id= "@+id/bind" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "work" an droid:text= "bind"/> <button android:id= "@+id/unbind" android:layout_width= "Wrap_content" an droid:layout_height= "Wrap_content" android:onclick= "work" android:text= "unbind"/> <button A Ndroid:id= "@+id/call" Android:layout_widTh= "Wrap_content" android:layout_height= "wrap_content" android:onclick= "work" android:text= "call"/&G T;</linearlayout>

Localservice.java file

Package Com.qf.act;import Android.app.service;import Android.content.intent;import android.os.binder;import Android.os.ibinder;import Android.util.log;public class LocalService extends Service {private static String Log = "LocalS Ervice ";p rivate int count = 0;private IBinder binder = new Myibinder (); @Overridepublic ibinder Onbind (Intent Intent) {Log. E (LOG, "Onbind"); return this.binder;} @Overridepublic boolean onunbind (Intent Intent) {log.e (Log, "Onunbind"); return true;} @Overridepublic void Onrebind (Intent Intent) {super.onrebind (Intent); LOG.E (LOG, "Onrebind");} @Overridepublic void OnCreate () {new Thread () {public void run () {LOG.E (Log, ' onCreate '); for (int i = 0; i <; i++) {count++;try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ();}}}}. Start (); Super.oncreate ();} public class Myibinder extends Binder {public int getcount () {return LocalService.this.getCount ();}} public int GetCount () {return this.count;} @Overridepublic void OnDestroy () {LOG.E (Log, "ondestRoy "); Super.ondestroy ();}} 

Mainactivity.java file

Package Com.qf.act;import Com.qf.act.localservice.myibinder;import Android.app.activity;import android.app.Service ; import Android.content.componentname;import Android.content.intent;import android.content.ServiceConnection; Import Android.os.bundle;import android.os.ibinder;import Android.util.log;import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {private Boolean isbind = False;private    Localservice.myibinder binder = null;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main);    } public void work (View v) {Intent Intent = new Intent ();    Intent.setclass (this, localservice.class);    Switch (V.getid ()) {case R.id.start:this.startservice (intent);    Break    Case R.id.stop:this.stopservice (Intent); Break;case R.id.bind:this.bindservice (Intent, Conn, service.bind_auto_create); Break;case r.id.unbind:if (IsBind = = True) {Unbindservice (conn); isbind = false;} Break;case r.id.call:if (This.binder = = null) return;int count = This.binder.getCount ();    Toast.maketext (This, "" +count, 1). Show (); break;}} Private Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name {LOG.E ("", "onservicedisconnected");} @Overridepublic void onserviceconnected (componentname name, IBinder service) {binder = (Myibinder) Service;isbind = true; }};}

Examples of operations:

1. Click the button to start the service

Log information: OnCreate

2. Click the button bind

Log information: Onbind

3. Click the button unbind

Log information: Onunbind

4. Click the button bind

Log information: Onrebind

When to call Onrebind when binding a service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.