上篇是第一種方法,這裡主要是第二種Context.bindService()方法
1. 開發步驟
1. 綁定服務
Intent intent=new Intent(getBaseContext(),MyService.class);
bindService(intent,connection, Context.BIND_AUTO_CREATE);
2. 解綁服務
unbindService(connection);
首先對于對于bindService()Method,
參數如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0NXYFhGd192UvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2Lc1TPnVmNwhlWqhmMiZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DOyYTMyETMxEDMxUDM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
各個參數解釋如下:
- 1.service:即通過intent指定的服務,與之前第一種方法一緻
- 2.conn:這是ServiceConnection的一個對象,其中有兩個 函數,分别是onServiceConnected()和onServiceDisconnected(),用來監聽通路者與Service之間的連接配接情況,當通路者與Service成功連接配接将回調onServiceConnected函數,當Service程序被crashed or killed(異常終止等)則回調onServiceDisconnected函數。
具體函數為:
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// method
}
@Override
public void onServiceDisconnected(ComponentName name) {
// method
}
};
- 3.flag:即綁定時是否自動建立Service,如上參數圖所示,0為不自動,BIND_AUTO_CREATE為自動建立
測試demo
MainActivity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent=new Intent(getBaseContext(),MyService.class);
Button btstart=(Button)findViewById(R.id.button1);
Button btstop=(Button)findViewById(R.id.button2);
Button btbind=(Button)findViewById(R.id.button3);
Button btunbind=(Button)findViewById(R.id.button4);
btstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//啟動服務
startService(intent);
}
});
btstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//停止服務
stopService(intent);
}
});
btbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindService(intent,connection, Context.BIND_AUTO_CREATE);
}
});
btunbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(connection);
}
});
}//onCreate
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// method
System.out.println("onServiceConnected");
}
@Override
public void onServiceDisconnected(ComponentName name) {
// method
System.out.println("onServiceDisconnected");
}
};
}
MyService:
public class MyService extends Service {
public MyService() {
}
//綁定時調用
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
System.out.println("Service Binded");
// throw new UnsupportedOperationException("Not yet implemented");
return new Binder();
}
// System.out 用于測試
//建立時調用
public void onCreate(){
super.onCreate();
System.out.println("Service Created");
Toast.makeText(this,"Service Created",Toast.LENGTH_LONG).show();
}
//Service 啟動時調用
public int onStartCommand(Intent intent,int flags,int startID){
System.out.println("Service Started");
Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
return START_STICKY;
}
//關閉時調用
public void onDestroy(){
super.onDestroy();
System.out.println("Service Destroyed");
Toast.makeText(this,"Service Destroyed",Toast.LENGTH_SHORT).show();
}
//解除綁定時調用
public boolean onUnbind(Intent intent){
System.out.println("Service Unbinded");
return true;
}
}
UI:
測試:
當按下綁定服務:
- 首先自動建立服務(flag那設定的),回調onCreate()函數,
- 之後綁定服務,回調onBind()函數
- 最後回調ServiceConnection裡面的onServiceConnection函數
之後按下解綁服務
- 回調服務裡面onUnbind()函數
- 之後回調服務裡面onDestroy()函數
2. 關于生命周期
在這裡可以清晰看到,
startService後,再綁定服務。當解綁服務時候,Service并沒有回調onDestroy()函數,
而綁定服務再解綁時,則會調用onDestroy()函數。
這是因為第一種情況,Service是由startService建立的,不是bindService。而第二種情況,則是bindService建立的Service。從這裡發現,當bindService時,如果服務已經建立,那麼解綁隻是解除通路者與Service的綁定,而Service并不會終止。
這裡關于生命周期,調用方法不同,周期也不同。
- 調用startService周期:onCreate()-onStartCommand()-onDestroy()
- 調用bindService周期:onCreate()-onBind()-Unbind()-onDestroy()
相關連結
Android-服務Service(1)-介紹及startService調用
Android-服務Service(2)-bind綁定Service及兩種生命周期
Android-服務Service(3)-IntentService