綁定服務時什麼時候調用onRebind,綁定調用onrebind

來源:互聯網
上載者:User

綁定服務時什麼時候調用onRebind,綁定調用onrebind

Serivce中onRebind被調用的時機很特別,想知道什麼時候onRebind被調用,可以接下面的次序來學習,最後自然就明白了!

1. 首先要知道,同一個服務既可能被啟動也可以被綁定;

2. Service中onRebind方法被調用,只要符合兩個必要條件就行

    (1)服務中onUnBind方法返回值為true

    (2)服務物件被解除綁定後沒有被銷毀,之後再次被綁定

。下面舉例說明:

    例1:同一個Activity對象

    先自啟動服務(onCreate, onStartCommand);再綁定服務(onBind); 再解除綁定服務(onUnBind)(由於服務被啟動過,所以Service中onDestroy不會被調用);再綁定服務, 這次綁定的服務物件是之前已經建立好的,所以這次綁定服務時就會調用onReBind方法了,並且本次不會調用onBind方法。

   例2:不是同一個Activity對象 

        開啟項目,啟動MainActivity, 在Activity中啟動服務(onCreate, onStartCommand),再綁定服務(onBind); 再解除綁定服務(onUnBind); 再接返回鍵銷毀MainActivity對象(onUnBind);再開啟項目啟動MainActivity;再綁定服務,這次綁定服務時會調用onReBind方法

 

 

程式碼範例:

activity_main.xml檔案

<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檔案

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檔案

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;}};}

 

操作樣本:

     1.點擊按鈕  啟動服務

       日誌資訊:       onCreate

    2.  點擊按鈕  bind

       日誌資訊:    onBind

    3.點擊按鈕  unbind

        日誌資訊:   onUnBind

    4.點擊按鈕  bind

        日誌資訊:  onReBind

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.