When to call onRebind and onrebind when binding a service

Source: Internet
Author: User

When to call onRebind and onrebind when binding a service

In Serivce, onRebind is called at a very special time. If you want to know when onRebind is called, you can learn in the next order. In the end, you will naturally understand it!

1. First, you must know that the same service may be started or bound;

2. The onRebind method in the Service is called, as long as the two necessary conditions are met.

(1) The onUnBind method in the service returns true.

(2) The service object is not destroyed after being unbound, and then bound again

. The following is an example:

Example 1: Same Activity object

Start the Service (onCreate, onStartCommand) First, bind the Service (onBind), and then unbind the Service (onUnBind) (because the Service has been started, onDestroy in the Service will not be called ); bind the service again. The bound service object is previously created. Therefore, the onReBind method will be called this time, and the onBind method will not be called this time.

Example 2: not the same Activity object

Open the project, start MainActivity, start the service (onCreate, onStartCommand) in the Activity, bind the Service (onBind), and unbind the Service (onUnBind ); the Return key is used to destroy the MainActivity object (onUnBind), start the project, and bind the service. The onReBind method is called this time.

 

 

Sample Code:

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="MergeRootFrame,Orientation" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android: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"        android:text="bind" />    <Button        android:id="@+id/unbind"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="work"        android:text="unbind" />    <Button        android:id="@+id/call"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="work"        android:text="call" /></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 = "LocalService";private 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 < 100; i++) {count++;try {Thread.sleep(1000);} 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;}};}

 

Operation example:

1. click the button to start the service.

Log information: onCreate

2. Click bind.

Log information: onBind

3. Click the unbind button.

Log information: onUnBind

4. Click bind.

Log information: onReBind

 

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.