1. 程式人生 > >我的onServiceDisconnected為什麼沒有被呼叫

我的onServiceDisconnected為什麼沒有被呼叫

有人會在些bound service的client的時候會利用onServiceDisconnected來釋放資源,但是並達不到預期效果。在呼叫unbindService後onServiceDisconnected並沒有被呼叫。

請仔細閱讀文件

public abstract void onServiceDisconnected 

Called when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed. This does not

 remove the ServiceConnection itself -- this binding to the service will remain active, and you will receive a call to  when the Service is next running.

Parameters
name The concrete component name of the service whose connection has been lost.

尤其是This typically happens when the process hosting the service has crashed or been killed. 

即當service所在程序crash或者被kill的時候,onServiceDisconnected才會被呼叫。

如果要嚴重的話,請使用adb shell kill pid 命令kill service來測試。