2

我为未处理的异常添加了一个异常处理程序,以知道应用程序在下一次应用程序启动时崩溃了:

@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 向我显示了一个弹出窗口,告诉我应用程序不再响应以及我是否想再等待。这不是很好的国际海事组织。任何提示,应用程序崩溃后如何正确退出?

4

2 回答 2

5

您可以保存原始异常处理程序,以便在对未处理的异常执行您自己的自定义操作后调用它。像这样的东西:

//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);
  }
于 2012-06-24T13:31:40.753 回答
0

我建议您将您的逻辑与ACRA集成,以便它可以处理崩溃报告和清理问题。

于 2012-06-24T13:26:34.063 回答