今天在做Launcher項目時,需要顯示所有app,並且點擊調用,在調用時,遇到一個崩潰問題。在此做一記錄。
問題:在本地寫的一個demo應用,裏面有一個bug,點擊這個demo應用,應用崩潰。在launcher應用中也點擊調用這個demo應用,demo應用崩潰後,我的launcher應用也跟着崩潰。但是異常日誌報了一些其它的錯誤 ,我這裏的錯誤是
Activity com.example.XXX .MainActivity has leaked IntentReceiver com.example.XXX.xxxView tha was originall registered here.Are you missing a call to unregisterReceiver()?
由於我的launcher程序中有用到Broadcast,所以我把所有註冊Broadcast和unregisterReceiver的地方全部註釋掉,但是問題依然沒有解決,還是會導致我的launcher應用崩潰。所以繼續查找原因後發現,在日誌中,有兩行黃色的日誌,
PID:1696 , TID:4083 , Application : system_process Tag : ActivityManager
Text : Force finishing activity 1 com.example.demo/.MainActivity
PID:1696 , TID:4083 ,Application : system_process Tag : ActivityManager
Text : Force finishing activity 2 com.example.XXX/.MainActivity
PID:27009, TID:27009 ,Application : com.example.demo Tag :Process
Text : Sending signal. PID:27009 ,SIG:9
PID:1696 , TID:4083 ,Application : system_process Tag : ActivityManager
Text :Process com.example.vibrator (pid 27009) has died
原因是:
寫代碼中爲了調試方便,我並沒有在mainfest.xml中將屬性設置爲 launcher 中的android.intent.category.HOME
當demo程序崩潰後,由系統manager殺死了demo進程,同時,也將調用這個app的進程跟着殺死,導致我的lanucher崩潰。
由於我的程序是一個launcher程序,故我在mainfest.xml文件中將程序設置爲launcher,再進行運行,這時,程序並沒有退出。日誌如下
PID:1696 , TID:4083 , Application : system_process Tag : ActivityManager
Text : Force finishing activity 1 com.example.demo/.MainActivity
這裏只打印了一次,說明,系統並沒有殺死我的launcher程序。
分析:當一個應用利用intent調用啓動另一個進程的app時,如果這個app崩潰,系統會自動檢測調用它的程序是不是一個launcher程序,如果是,那麼就不將這個調用它的程序殺死,如果不是,則將調用者的進程也殺死。
對於跨進程通訊,這裏不做分析,原因是 跨進程通訊是通過aidl調用Service接口來進行通訊,同時系統架構中設有程序保活功能。故即使調用另一個進程來通訊,即使它有bug,也不會造成本app跟着崩潰。(注:這裏我也不懂,只是領導跟我這樣解釋的)