Service之onStartCommand剖析筆記

個人博客地址 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五嫂;

想閱讀作者的更多文章,可以查看我 個人博客 和公共號:

振興書城

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末肯尺,一起剝皮案震驚了整個濱河市沃缘,隨后出現的幾起案子,更是在濱河造成了極大的恐慌则吟,老刑警劉巖槐臀,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異氓仲,居然都是意外死亡水慨,警方通過查閱死者的電腦和手機得糜,發(fā)現死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晰洒,“玉大人朝抖,你說我怎么就攤上這事』肚辏” “怎么了槽棍?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長抬驴。 經常有香客問我炼七,道長,這世上最難降的妖魔是什么布持? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任豌拙,我火速辦了婚禮,結果婚禮上题暖,老公的妹妹穿的比我還像新娘按傅。我一直安慰自己,他們只是感情好胧卤,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布唯绍。 她就那樣靜靜地躺著,像睡著了一般枝誊。 火紅的嫁衣襯著肌膚如雪况芒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天叶撒,我揣著相機與錄音绝骚,去河邊找鬼。 笑死祠够,一個胖子當著我的面吹牛压汪,可吹牛的內容都是我干的。 我是一名探鬼主播古瓤,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼止剖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了落君?” 一聲冷哼從身側響起穿香,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎叽奥,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體痛侍,經...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡朝氓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年魔市,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赵哲。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡待德,死狀恐怖,靈堂內的尸體忽然破棺而出枫夺,到底是詐尸還是另有隱情将宪,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布橡庞,位于F島的核電站较坛,受9級特大地震影響,放射性物質發(fā)生泄漏扒最。R本人自食惡果不足惜丑勤,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望吧趣。 院中可真熱鬧法竞,春花似錦、人聲如沸强挫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俯渤。三九已至呆细,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間稠诲,已是汗流浹背侦鹏。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留臀叙,地道東北人略水。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像劝萤,于是被迫代替她去往敵國和親渊涝。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內容