在我的应用程序中,我想设置未捕获的异常处理程序,以便我可以在不可预见的崩溃事件中执行操作。 (我想做关闭套接字,清除通知......等等。)
Thread.currentThread().setDefaultUncaughtExceptionHandler(sDefaultThreadHandler);
,其中
private static UncaughtExceptionHandler sDefaultThreadHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// i want ACRA to log here, then clear notifications, close out connections, cancel asynctasks...etc.
// DO NOT REMOVE or else your app will hang when there is a crash.
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
};
问题是,我希望ACRA在流程退出前报告。我该如何做到这一点?
答案 0 :(得分:3)
哦等等,恩。我发现ACRA使用默认的异常处理程序(根据https://github.com/ACRA/acra/blob/master/src/main/java/org/acra/ErrorReporter.java#L201),这意味着如果你自己的线程有一个线程异常处理程序,那将首先使用它。
Thread.currentThread().setUncaughtExceptionHandler(mYourOwnThreadHandler);
如果您确实需要使用ACRA,那么在您覆盖的uncaughtException()
方法内,只需通过调用Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, ex);