您无法从此处执行任何 UI 操作。只需启动另一个活动/启动屏幕即可。传递一个额外的意图来表示崩溃并在该活动中显示对话框。
/*
* (non-Javadoc)
*
* @see
* java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.
* lang.Thread, java.lang.Throwable)
*/
@Override
public void uncaughtException(Thread t, final Throwable e) {
StackTraceElement[] arr = e.getStackTrace();
final StringBuffer report = new StringBuffer(e.toString());
final String lineSeperator = "-------------------------------\n\n";
report.append(DOUBLE_LINE_SEP);
report.append("--------- Stack trace ---------\n\n");
for (int i = 0; i < arr.length; i++) {
report.append( " ");
report.append(arr[i].toString());
report.append(SINGLE_LINE_SEP);
}
report.append(lineSeperator);
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report.append("--------- Cause ---------\n\n");
Throwable cause = e.getCause();
if (cause != null) {
report.append(cause.toString());
report.append(DOUBLE_LINE_SEP);
arr = cause.getStackTrace();
for (int i = 0; i < arr.length; i++) {
report.append(" ");
report.append(arr[i].toString());
report.append(SINGLE_LINE_SEP);
}
}
// Getting the Device brand,model and sdk verion details.
report.append(lineSeperator);
report.append("--------- Device ---------\n\n");
report.append("Brand: ");
report.append(Build.BRAND);
report.append(SINGLE_LINE_SEP);
report.append("Device: ");
report.append(Build.DEVICE);
report.append(SINGLE_LINE_SEP);
report.append("Model: ");
report.append(Build.MODEL);
report.append(SINGLE_LINE_SEP);
report.append("Id: ");
report.append(Build.ID);
report.append(SINGLE_LINE_SEP);
report.append("Product: ");
report.append(Build.PRODUCT);
report.append(SINGLE_LINE_SEP);
report.append(lineSeperator);
report.append("--------- Firmware ---------\n\n");
report.append("SDK: ");
report.append(Build.VERSION.SDK);
report.append(SINGLE_LINE_SEP);
report.append("Release: ");
report.append(Build.VERSION.RELEASE);
report.append(SINGLE_LINE_SEP);
report.append("Incremental: ");
report.append(Build.VERSION.INCREMENTAL);
report.append(SINGLE_LINE_SEP);
report.append(lineSeperator);
Log.e("Report ::", report.toString());
Intent crashedIntent = new Intent(BaseActivity.this, SplashActivity.class);
crashedIntent.putExtra(EXTRA_CRASHED_FLAG, "Unexpected Error occurred.");
crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
crashedIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(crashedIntent);
System.exit(0);
// If you don't kill the VM here the app goes into limbo
}
另请参阅:
实例化 AlertDialog 的 Android UncaughtExceptionHandler 中断 https://stackoverflow.com/questions/5519347/android-uncaughtexceptionhandler-that-instantiates-an-alertdialog-breaks
Toast 未显示在 UnCaughtExceptionHandler 中 https://stackoverflow.com/questions/11609640/toast-not-showing-up-in-uncaughtexceptionhandler
如果主线程崩溃,如何从 UncaughtExceptionHandler 启动活动? https://stackoverflow.com/questions/2222396/how-to-start-activity-from-uncaughtexceptionhandler-if-this-is-main-thread-crash
我是怎么做的:
我有一个扩展 Activity 的 BaseActivity,在活动的 onCreate 中我设置了 UncaughtExceptionHandler。我的所有活动都扩展了 BaseActivity 而不是 Activity。
Keys
- 您不能在中设置异常处理程序
Application.onCreate
,相反,您应该创建一个BaseActivity
并将其设置在onCreate
它的方法。
- 启动 SplashActivity 后,我们应该调用
System.exit(0)
- 我们无法保存错误实例以将其共享给
SplashActivity
,因为它会被销毁,所以我们可以传递一些错误消息或将其保留在文件中。