1. 程式人生 > >Service的onRebind(Intent intent)的呼叫時機

Service的onRebind(Intent intent)的呼叫時機

官方解釋如下:

Called when new clients have connected to the service, after it had previously been notified that all
had disconnected in its onUnbind. This will only be called if the implementation of onUnbind was overridden
to return true.

Service中onRebind方法被呼叫的兩個必要條件

(1)服務中onUnBind方法返回值為true
(2)服務物件被解綁後沒有被銷燬,之後再次被繫結

模擬呼叫流程:

  1. 先啟動服務(onCreate, onStartCommand);
03-08 16:46:06.992 24411-24411/com.lsw.demo I/MyService: onCreate: 
03-08 16:46:07.027 24411-24411/com.lsw.demo I/MyService: onStartCommand: 
  1. 再繫結服務(onBind);
03-08 16:46:10.771 24411-24411/com.lsw.demo I/MyService: onBind: 
  1. 再解除繫結服務(onUnBind)(由於服務被啟動過,所以Service中onDestroy不會被呼叫);
03-08 16:56:45.922 24411-24411/com.lsw.demo I/MyService: onUnbind: 
  1. 再繫結服務;
03-08 16:57:05.040 24411-24411/com.lsw.demo I/MyService: onRebind: 

這次繫結的服務物件是之前已經建立好的,所以這次繫結服務時就會呼叫onReBind方法了,並且本次不會呼叫onBind方法。

相關程式碼見如下網站:
link