Managing the Lifecycle of a Bound Service
原文URL:Managing the Lifecycle of a Bound Service
Bound Service的生命周期管理
當一個service與所有的client解除綁定關系后,Android 系統(tǒng)會銷毀它(除非該Service也是用onStartCommand()方法啟動的)。所以,如果你的service僅僅只是一個綁定service的話,你不必去管理它的生命周期,因為Android系統(tǒng)會基于它是否綁定了客戶端來替你管理它。
然而,如果你重寫onStartCommand()回調方法,那么你必須顯示的停止該service ,因為該service被系統(tǒng)認為是用onStartCommand()方法啟動的。這種情況下,不管該service 是否綁定有任何client,該service 會一直運行到調用stopSelf()方法終止它自己或者其他的組件調用stopService()方法終止它為止。
另外,如果你的service是被啟動的,并且接受綁定,當系統(tǒng)調用你的onUnbind()方法時,如果你想下次當一個client綁定到該service時調用onRebind()方法(而不是調用onBind()方法),你可以在onUnbind()方法中選擇返回true。onRebind()返回void,但是client仍然可以在它的onServiceConnected()方法中接收到IBinder對象。下圖表明了這種生命周期邏輯。
Figure 1.?The lifecycle for a service that is started and also allows binding.
圖1 ?一個被啟動的并且可以接受綁定的Service 的生命周期
For more information about the lifecycle of a started service, see?theServices?document.
總結
以上是生活随笔為你收集整理的Managing the Lifecycle of a Bound Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bound Services
- 下一篇: VNC怎么和宿主机共享粘贴板(整理)