2012-06-24 39 views
2

我增加了一個的ExceptionHandler未處理異常,要知道,應用程序崩潰的下一個應用程序啓動:的android:如何正確地退出程序上的錯誤

@Override 
public void uncaughtException(Thread thread, Throwable ex) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString("crashed", "yes"); 
    editor.commit(); 
} 

我加入經處理:

Thread.setDefaultUncaughtExceptionHandler(this); 

現在是一點:因爲我添加了處理程序,該應用程序行爲不同的錯誤:

  • 之前我添加了處理程序我有一個彈出窗口告訴我,該應用程序崩潰。

  • 現在,添加處理程序後,應用程序只是凍結,過了一會兒,android向我顯示一個彈出窗口,告訴我該應用程序不再響應,並且是否要再等待。這不是好的國際海事組織。任何提示,如何在應用程序崩潰後正確退出?

+0

從一個活動退出,你只需要調用'finish()'。 –

+0

@Styx如果我叫完成()在我的錯誤處理程序,根本沒有彈出顯示出來。這不是我想要的。 – stoefln

回答

0

我建議您將您的邏輯與ACRA相集成,以便它能夠處理崩潰報告和清理問題。

+0

我已經使用Crittercism,它做了偉大的工作,但Crittercism.didCrashOnLastAppLoad()方法似乎是越野車...... – stoefln

4

可以保存原來的異常處理程序,這樣就可以把它叫做你與未處理的異常做自己的自定義操作完成之後。這樣的事情:

//Inside UncaughtExceptionHandler.java: 
… 
private UncaughtExceptionHandler defaultUEH; 

public DefaultExceptionHandler() 
    { 
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); 
    } 

@Override 
public void uncaughtException(Thread t, Throwable e) 
    { 
    //do anything you wish about the Throwable e :getStackTrace(),getCause(),getClass()... 
    //call the original uncaught exception handler: 
    defaultUEH.uncaughtException(t, e); 
    } 
+0

,改變默認的行爲爲好,不知道爲什麼,雖然... – stoefln

+0

做什麼你意思是? –