startService()
04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
startService()
04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
bindService()
04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:11:29.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
bindService()
null
startService()
04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
stopService()
null
unBindService()
04-18 22:11:23.011 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:11:23.019 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:25.920 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:11:29.075 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:11:29.083 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:13:43.525 15225-15225/com.github.androidfdm E/SampleService: onUnBind
stopService()
04-18 22:13:39.773 15225-15225/com.github.androidfdm E/SampleService: onCreate
04-18 22:13:39.781 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:13:39.919 15225-15225/com.github.androidfdm E/SampleService: onStartCommand
04-18 22:13:41.507 15225-15225/com.github.androidfdm E/SampleService: onBind
04-18 22:13:41.516 15225-15225/com.github.androidfdm E/MainActivity: Im binder!
04-18 22:13:43.525 15225-15225/com.github.androidfdm E/SampleService: onUnBind
04-18 22:14:37.669 15225-15225/com.github.androidfdm E/SampleService: onDestroy
結(jié)論:
start啟動的Service必須由stop來銷毀,若在start之后bind了則必須unbind之后再stop袭异,若多次bind不會執(zhí)行任何回調(diào)虑啤,多次start回調(diào)用多次startcommand并且多次傳入Intent劲适!
onStartCommand()返回值
onStartCommand()方法有一個int的返回值峭梳,這個返回值標識服務關閉后系統(tǒng)的后續(xù)操作箱叁。
返回值有以下幾種:
Service.START_STICKY肖揣,啟動后的服務被殺死拭嫁,系統(tǒng)會自動重建服務并調(diào)用on onStartCommand()可免,但是不會傳入最后一個Intent(Service可能多次執(zhí)行onStartCommand)抓于,會傳入一個空的Intent,使用這個標記要注意對Intent的判空處理浇借。這個標記適用于太依靠外界數(shù)據(jù)Intent捉撮,在特定的時間,有明確的啟動和關閉的服務妇垢,例如后臺運行的音樂播放巾遭。
Service.START_NOT_STICKY,啟動后的服務被殺死闯估,系統(tǒng)不會自動重新創(chuàng)建服務灼舍。這個標記是最安全的,適用于依賴外界數(shù)據(jù)Intent的服務睬愤,需要完全執(zhí)行的服務片仿。
Service.START_REDELIVER_INTENT,啟動后的服務被殺死尤辱,系統(tǒng)會重新創(chuàng)建服務并調(diào)用onStartCommand()砂豌,同時會傳入最后一個Intent。這個標記適用于可恢復繼續(xù)執(zhí)行的任務光督,比如說下載文件阳距。
Service.START_STICKY_COMPATIBILITY,啟動后的服務被殺死结借,不能保證系統(tǒng)一定會重新創(chuàng)建Service筐摘。