1. 程式人生 > >Android 利用Intent啟動另一個app應用時,一個崩潰的問題

Android 利用Intent啟動另一個app應用時,一個崩潰的問題

今天在做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跟著崩潰。(注:這裡我也不懂,只是領導跟我這樣解釋的)