Scheduling restart of crashed service 如何提升Service的優先級避免被殺死或者殺死後如何再次重啓Service?

    08-16 11:38:23.000: W/ActivityManager(389): Scheduling restart of crashed servicehtml

1. ActivityManager 與 service之間的關係android

2. service正常關閉與異常關閉的狀況app

 

【http://www.cnblogs.com/wlrhnh/p/3529926.html】post

如何提升Service的優先級避免被殺死或者殺死後如何再次重啓Service?this

咱們知道,當進程長期不活動時,若是系統資源吃緊,會殺死一些Service,或不可見的Activity等所在的進程。 url

如何避免Service被系統殺死,隨便在網上搜一下,都能搜到好幾種方法,可是每一種方法都有不一樣的適用環境。spa

1. 添加android:persistent="true"rest

添加android:persistent="true"到AndroidManifest.xml,Google文檔描述以下:code

Whether or not the application should remain running at all times-true" if it should, and "false"if not. The default value is "false". Applications should not normally set this flag; persistence mode is intended only for certain system applications.可見這個屬性不能隨便用,到目前爲止,我所發現使用該屬性的應用只有Phone,並且使用是要求權限的,因此這個屬性對第三方應用來講意義不是很大;orm

2. 設置onStartCommand()的返回值

這個思路比較有用,咱們着重分析一下,該方法有四種返回值:

 

TART_STICKY:kill後會被重啓,可是重啓後調用onStarfCommand()傳進來的Intent參數爲null,說明被kill的時候沒有保存Intent;

START_NOT_STICKY:kill以後不會被重啓;

相關文章
相關標籤/搜索