當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 綁定服務(wù)時(shí)什么時(shí)候調(diào)用onRebind
Serivce中onRebind被調(diào)用的時(shí)機(jī)很特別,想知道什么時(shí)候onRebind被調(diào)用,可以接下面的次序來(lái)學(xué)習(xí),后自然就明白了!
1. 首先要知道,同一個(gè)服務(wù)既可能被啟動(dòng)也可以被綁定;
2. Service中onRebind方法被調(diào)用,只要符合兩個(gè)必要條件就行
<1>服務(wù)中onUnBind方法返回值為true<2>服務(wù)對(duì)象被解綁后沒(méi)有被銷毀,之后再次被綁定。下面舉例說(shuō)明:
例1:同一個(gè)Activity對(duì)象
先自啟動(dòng)服務(wù)(onCreate, onStartCommand);再綁定服務(wù)(onBind); 再解除綁定服務(wù)(onUnBind)(由于服務(wù)被啟動(dòng)過(guò),所以Service中onDestroy不會(huì)被調(diào)用);再綁定服務(wù), 這次綁定的服務(wù)對(duì)象是之前已經(jīng)創(chuàng)建好的,所以這次綁定服務(wù)時(shí)就會(huì)調(diào)用onReBind方法了,并且本次不會(huì)調(diào)用onBind方法。
例2:不是同一個(gè)Activity對(duì)象
打開(kāi)項(xiàng)目,啟動(dòng)MainActivity, 在Activity中啟動(dòng)服務(wù)(onCreate, onStartCommand),再綁定服務(wù)(onBind); 再解除綁定服務(wù)(onUnBind); 再接返回鍵銷毀MainActivity對(duì)象(onUnBind);再打開(kāi)項(xiàng)目啟動(dòng)MainActivity;再綁定服務(wù),這次綁定服務(wù)時(shí)會(huì)調(diào)用onReBind方法
代碼示例:
//activity_main.xml文件
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:tools="//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>
布局文件運(yùn)行的界面
//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();
@Override
public IBinder onBind(Intent intent) {
Log.e(LOG, "onBind");
return this.binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(LOG, "onUnBind");
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.e(LOG, "onRebind");
}
@Override
public 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;
}
@Override
public 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() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("", "onServiceDisconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (MyIbinder) service;
isBind = true;
}
};
}
操作示例:
1.點(diǎn)擊按鈕 啟動(dòng)服務(wù)
日志信息: onCreate
2. 點(diǎn)擊按鈕 bind
日志信息: onBind
3.點(diǎn)擊按鈕 unbind
日志信息: onUnBind
4.點(diǎn)擊按鈕 bind
日志信息: onReBind