Android APP crash 的时候会弹出一个 Force close Dialog,这篇文章主要记录一下该 Dialog 的显示流程,以及由此引申出来的自定义 APP crash 处理流程
- Force close Dialog 的显示流程
- 自定义
Thread.UncaughtExceptionHandler
处理 APP crash
Force close 处理流程
Android 主要使用Thread.UncaughtExceptionHandler
机制来处理 APP Crash,这个defaultUncaughtException
定义在 RuntimeInit.commonInit()
private static final void commonInit() {
......
/* set default handler; this applies to all threads in the VM */
Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler());
......
}
private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
try {
......
if (mApplicationObject == null) {
Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
} else {
StringBuilder message = new StringBuilder();
// 1) 把Exception信息打印到logcat中
message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n");
final String processName = ActivityThread.currentProcessName();