個人博客地址 http://dandanlove.com/
Service是我們學習Android的基石之一体谒,它在移動應用程序中使用非常廣泛丙者。比如應用定位,push消息营密,內存流量監(jiān)聽等等。
記得大四那年在公司實習的時候目锭,我做的第一個調研就是怎么讓接受服務器push的Service不被kill掉(或kill后實現重新啟動)评汰。在調研的過程中就了解到如果Service的onStartCommand方法返回值為START_STICKY時,那么Service在不久后就會嘗試重啟痢虹。被去。〗蔽ǎ〔依拢現在自己重溫Service知識點,想將其記錄一下讓自己對其印象更加深刻。
我們常用的onStartCommand返回值有START_STICKY坯墨、START_NOT_STICKY寂汇、START_REDELIVER_INTENT等等。
START_STICKY:
=======
Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.
This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.
- 如果在onStartCommand(Intent, int, int)返回恒為START_STICKY捣染。那么假設這個服務所在的進程被殺掉骄瓣,那么開始在啟動onStartCommand(Intent, int, int)方方法是傳過來的Intent不會被保留。稍后系統(tǒng)會嘗試重新創(chuàng)建這個service耍攘,并保證開始在創(chuàng)建新的service實例后調用onStartCommand(Intent, int, int)方法榕栏。如果這里沒有任何掛起的服務調用service,那么系統(tǒng)會通過空Intent調用onStartCommand(Intent, int, int)方法蕾各。此模式對于明確啟動和停止運行任意時間段(例如執(zhí)行背景音樂播放的服務)的事物有意義扒磁。
- getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.ECLAIR時默認為START_STICKY
START_STICKY_COMPATIBILITY:
Constant to return from onStartCommand(Intent, int, int): compatibility version of START_STICKY that does not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.
- 對START_STICKY的兼容,不保證service殺掉后調用onStartCommand(Intent, int, int)式曲。
- getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.ECLAIR時默認為START_STICKY_COMPATIBILITY
START_NOT_STICKY:
Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context.startService(Intent). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.
如果在onStartCommand(Intent, int, int)返回恒為START_STICKY妨托。那么假設這個服務所在的進程被殺掉,而且沒有一個新的intent啟動它检访。那么服務的狀態(tài)將不會被保留始鱼,直到一個新的顯示調用 Context.startService(Intent)。如果沒有一個掛起的Intent要傳遞脆贵,否則系統(tǒng)不會重建服務医清。
START_REDELIVER_INTENT:
Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).
如果在onStartCommand(Intent, int, int)返回恒為START_STICKY。那么假設這個服務所在的進程被殺掉,那么這個服務將會重啟并且將最后一個傳遞的Intent再次傳遞給onStartCommand(Intent, int, int)卖氨。這個Intent將會一直保留直到當前service調用stopSelf(int)
總結:
- 如果希望Service一直存活并且保留上次啟動它的intent的數據会烙,那么return START_REDELIVER_INTENT;
- 如果只希望Service一直存活不需要intent中的數據筒捺,那么return START_STICKY柏腻;
- 如果希望Service執(zhí)行完指定的任務后銷毀,那么return START_NOT_STICKY系吭;
- 如果沒有什么要求那么直接return super.onStartCommand五嫂;
想閱讀作者的更多文章,可以查看我 個人博客 和公共號: