|
概要
工作中遇到一個問題:監(jiān)控應用的后臺Service需要一直運行不退出。為了選用合適的方法,搜索了網(wǎng)上常用保持進程或者Service不被殺死的方法,監(jiān)聽開機廣播、解鎖屏廣播等有規(guī)律廣播是很好的選擇。當應用運行在android2.x版本上時一切正常,但是在android4.x版本上發(fā)現(xiàn)應用被用戶在系統(tǒng)Settings中強制force stop后,無法監(jiān)聽到此類廣播,導致監(jiān)控應用不能正常運行。
調(diào)查發(fā)現(xiàn),這是android系統(tǒng)在3.1版本以后為了加強系統(tǒng)安全性和優(yōu)化性能對系統(tǒng)廣播進行了限制。
Service運行不退出的常用方法
總結(jié)常用保證進程或者Service后臺運行不退出的方法如下:
1. 應用監(jiān)控開機廣播boot_completed、解鎖屏廣播user_present、網(wǎng)絡連接狀態(tài)改變廣播CONNECTIVITY_CHANGE等有規(guī)律的系統(tǒng)廣播
--android3.1以后,首次安裝未啟動或者用戶強制force stop后,應用無法監(jiān)聽到
--應用啟動后,可以接收到上述廣播,但是會導致手機啟動變慢,耗電明顯
2. 放到系統(tǒng)應用system/app/或者system/priv-app/目錄下永遠可以監(jiān)聽1中的廣播
--僅適用于預裝應用,或者手機已經(jīng)root可以操作系統(tǒng)目錄
3. 設置應用的application屬性persistent為true,保證應用開機后一直后臺運行
--僅適用于系統(tǒng)應用,第三方應用無效果
4. 開啟兩個獨立應用進程,相互監(jiān)控,一方被kill,由對方立即啟動
--一個應用內(nèi)開啟兩個進程,android3.1以后被用戶force stop后依然無法監(jiān)聽到1中的廣播
--開啟兩個獨立應用進程,首次啟動后可以實現(xiàn)相互監(jiān)控。但是用戶體驗不好,需要安裝兩個apk。
5. Service中調(diào)用AlarmManager的定時器功能,周期性檢查啟動Service
--由于pending Intent由系統(tǒng)AlarmManager發(fā)送,android3.1以后應用被force stop后無法收到
--定期發(fā)送造成應用耗電明顯
6. Service內(nèi)部處理,保證不退出
--在onStartCommand中設置start_sticky的返回值,可以保證Service因內(nèi)存不足被kill、內(nèi)存可用時被重啟
--在onDestory時重啟該Service
--提高Service優(yōu)先級:調(diào)用startForeground設置service為前臺
--應用被force stop后這些方法無效
不過在android3.1以后的版本上,對于第三方APK,上述幾種方法僅能盡量保證進程或者service不退出,但不是100%保證。
Android對于系統(tǒng)廣播的控制
從Android3.1開始,android對于系統(tǒng)發(fā)送的廣播進行了限制,原文如下:
Launch controls on stoppedapplications
Starting from Android 3.1, the system's package manager keeps track ofapplications that are in a stopped state and provides a means
of controllingtheir launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity'sstopped state. The system manages those two stopped states
separately.
The platform defines two new intent flags that let a sender specifywhether the Intent should be allowed to activate components in
stoppedapplication.
FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped applications in the list of potential targetsto resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped applications from the list of potentialtargets.
When neither or both of these flags is defined in an intent, the defaultbehavior is to include filters of stopped applications in
the list ofpotential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all broadcastintents. It does this to prevent broadcasts from background
services frominadvertently or unnecessarily launching components of stoppped applications.A background service or application can
override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcastintents that should be allowed to activate stopped
applications.
Applications are in a stopped state when they are first installed but are notyet launched and when they are manually stopped by the
user (in ManageApplications).
Android官方API說明:http://developer./about/versions/android-3.1.html#launchcontrols
對于上述描述總結(jié)如下:
1.在Android3.1以后版本添加了標志FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES,用于區(qū)分發(fā)送廣播時是否啟動激活那些未啟動過或者被用戶force stop的應用組件。當兩個Flag都不設置或都設置的時候,默認操作是FLAG_INCLUDE_STOPPED_PACKAGES。
2.系統(tǒng)向所有的Intent的廣播添加了FL??AG_EXCLUDE_STOPPED_PACKAGES標志。它這樣做是為了防止廣播無意中的或不必要地開啟組件的stoppped應用程序的后臺服務。這樣可以優(yōu)化系統(tǒng)性能,提高安全性,不過對于監(jiān)控類軟件是一個沉重的打擊。
3.用戶給自定義的廣播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES,用于啟動stop狀態(tài)的應用組件。但是系統(tǒng)自帶的廣播intent,我們無能為力。
綜上,在android3.1以后,系統(tǒng)加強了廣播的限制,對于進程或者service長時間運行不退出沒有有效的解決方案。總體來看,啟動兩個獨立應用進程是一個不錯的選擇,一旦用戶啟動應用后,即使點擊force stop,也可以由另一個應用啟動。概要
工作中遇到一個問題:監(jiān)控應用的后臺Service需要一直運行不退出。為了選用合適的方法,搜索了網(wǎng)上常用保持進程或者Service不被殺死的方法,監(jiān)聽開機廣播、解鎖屏廣播等有規(guī)律廣播是很好的選擇。當應用運行在android2.x版本上時一切正常,但是在android4.x版本上發(fā)現(xiàn)應用被用戶在系統(tǒng)Settings中強制force stop后,無法監(jiān)聽到此類廣播,導致監(jiān)控應用不能正常運行。
調(diào)查發(fā)現(xiàn),這是android系統(tǒng)在3.1版本以后為了加強系統(tǒng)安全性和優(yōu)化性能對系統(tǒng)廣播進行了限制。
Service運行不退出的常用方法
總結(jié)常用保證進程或者Service后臺運行不退出的方法如下:
1. 應用監(jiān)控開機廣播boot_completed、解鎖屏廣播user_present、網(wǎng)絡連接狀態(tài)改變廣播CONNECTIVITY_CHANGE等有規(guī)律的系統(tǒng)廣播
--android3.1以后,首次安裝未啟動或者用戶強制force stop后,應用無法監(jiān)聽到
--應用啟動后,可以接收到上述廣播,但是會導致手機啟動變慢,耗電明顯
2. 放到系統(tǒng)應用system/app/或者system/priv-app/目錄下永遠可以監(jiān)聽1中的廣播
--僅適用于預裝應用,或者手機已經(jīng)root可以操作系統(tǒng)目錄
3. 設置應用的application屬性persistent為true,保證應用開機后一直后臺運行
--僅適用于系統(tǒng)應用,第三方應用無效果
4. 開啟兩個獨立應用進程,相互監(jiān)控,一方被kill,由對方立即啟動
--一個應用內(nèi)開啟兩個進程,android3.1以后被用戶force stop后依然無法監(jiān)聽到1中的廣播
--開啟兩個獨立應用進程,首次啟動后可以實現(xiàn)相互監(jiān)控。但是用戶體驗不好,需要安裝兩個apk。
5. Service中調(diào)用AlarmManager的定時器功能,周期性檢查啟動Service
--由于pending Intent由系統(tǒng)AlarmManager發(fā)送,android3.1以后應用被force stop后無法收到
--定期發(fā)送造成應用耗電明顯
6. Service內(nèi)部處理,保證不退出
--在onStartCommand中設置start_sticky的返回值,可以保證Service因內(nèi)存不足被kill、內(nèi)存可用時被重啟
--在onDestory時重啟該Service
--提高Service優(yōu)先級:調(diào)用startForeground設置service為前臺
--應用被force stop后這些方法無效
不過在android3.1以后的版本上,對于第三方APK,上述幾種方法僅能盡量保證進程或者service不退出,但不是100%保證。
Android對于系統(tǒng)廣播的控制
從Android3.1開始,android對于系統(tǒng)發(fā)送的廣播進行了限制,原文如下:
Launch controls on stoppedapplications
Starting from Android 3.1, the system's package manager keeps track ofapplications that are in a stopped state and provides a means
of controllingtheir launch from background processes and other applications.
Note that an application's stopped state is not the same as an Activity'sstopped state. The system manages those two stopped states
separately.
The platform defines two new intent flags that let a sender specifywhether the Intent should be allowed to activate components in
stoppedapplication.
FLAG_INCLUDE_STOPPED_PACKAGES —Include intent filters of stopped applications in the list of potential targetsto resolve against.
FLAG_EXCLUDE_STOPPED_PACKAGES —Exclude intent filters of stopped applications from the list of potentialtargets.
When neither or both of these flags is defined in an intent, the defaultbehavior is to include filters of stopped applications in
the list ofpotential targets.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGESto all broadcastintents. It does this to prevent broadcasts from background
services frominadvertently or unnecessarily launching components of stoppped applications.A background service or application can
override this behavior by adding theFLAG_INCLUDE_STOPPED_PACKAGES flag to broadcastintents that should be allowed to activate stopped
applications.
Applications are in a stopped state when they are first installed but are notyet launched and when they are manually stopped by the
user (in ManageApplications).
Android官方API說明:http://developer./about/versions/android-3.1.html#launchcontrols
對于上述描述總結(jié)如下:
1.在Android3.1以后版本添加了標志FLAG_INCLUDE_STOPPED_PACKAGES和FLAG_EXCLUDE_STOPPED_PACKAGES,用于區(qū)分發(fā)送廣播時是否啟動激活那些未啟動過或者被用戶force stop的應用組件。當兩個Flag都不設置或都設置的時候,默認操作是FLAG_INCLUDE_STOPPED_PACKAGES。
2.系統(tǒng)向所有的Intent的廣播添加了FL??AG_EXCLUDE_STOPPED_PACKAGES標志。它這樣做是為了防止廣播無意中的或不必要地開啟組件的stoppped應用程序的后臺服務。這樣可以優(yōu)化系統(tǒng)性能,提高安全性,不過對于監(jiān)控類軟件是一個沉重的打擊。
3.用戶給自定義的廣播Intent添加FLAG_INCLUDE_STOPPED_PACKAGES,用于啟動stop狀態(tài)的應用組件。但是系統(tǒng)自帶的廣播intent,我們無能為力。
綜上,在android3.1以后,系統(tǒng)加強了廣播的限制,對于進程或者service長時間運行不退出沒有有效的解決方案??傮w來看,啟動兩個獨立應用進程是一個不錯的選擇,一旦用戶啟動應用后,即使點擊force stop,也可以由另一個應用啟動。
|