在 Android 中设置全局未捕获异常处理程序的理想方法

我想为 Android 应用程序中的所有线程设置一个全局未捕获异常处理程序。因此,在我的 Application子类中,我将 Thread.UncaughtExceptionHandler的一个实现设置为未捕获异常的默认处理程序。

Thread.setDefaultUncaughtExceptionHandler(
new DefaultExceptionHandler(this));

在我的实现中,我试图显示一个显示适当异常消息的 AlertDialog

然而,这似乎并不奏效。无论何时,当任何未处理的线程抛出异常时,我都会看到股票,OS-default 对话框(“对不起!- 应用程序已停止-意外对话框”)。

为未捕获的异常设置默认处理程序的正确和理想的方法是什么?

46793 次浏览

I think to disable that in your uncaughtException() method do not call previousHandler.uncaughtException() where previousHandler is set by

previousHandler = Thread.getDefaultUncaughtExceptionHandler();

That should be all you need to do. (Make sure you cause the process to halt afterward -- things could be in an uncertain state.)

The first thing to check is whether the Android handler is still getting called. It's possible that your version is being called but failing fatally and the system_server is showing a generic dialog when it sees the process crash.

Add some log messages at the top of your handler to see if it's getting there. Print the result from getDefaultUncaughtExceptionHandler and then throw an uncaught exception to cause a crash. Keep an eye on the logcat output to see what's going on.

FWIW I know this is slightly off-topic, but we've been using Crittercism's free plan with success. They also offer some premium features, like handling the exception so the app doesn't crash.

In the free version, the user still sees the crash, but at least I get the email and the stack trace.

We also use the iOS version (but I've heard from my colleagues that it is not quite as good).


Here are similar questions:

It doesn't work until you call

android.os.Process.killProcess(android.os.Process.myPid());

at the very end of your UncaughtExceptionHandler.

I posted the simple solution for custom handling of Android crashes a long ago. It's a little hacky however it works on all Android versions (including the Lollipop).

First a little bit of theory. The main issues when you use uncaught exception handler in Android come with the exceptions thrown in the main (aka UI) thread. And here is why. When the app starts system calls ActivityThread.main method which prepares and starts the Main looper of your app:

public static void main(String[] args) {
…
…
Looper.prepareMainLooper();
…
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}

Main looper is responsible for processing messages posted in the UI thread (including all messages related to UI rendering and interaction). If an exception is thrown in the UI thread it will be caught by your exception handler, but since you're out of loop() method you won't be able to show any dialog or activity to the user as there's no one left to process UI messages for you.

The proposed solution is quite simple. We run Looper.loop method by our own and surround it with try-catch block. When an exception is caught we process it as we want (for example start our custom report activity) and call Looper.loop method again.

The following method demonstrates this technique (it should be called from the Application.onCreate listener):

private void startCatcher() {
UncaughtExceptionHandler systemUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler();


// the following handler is used to catch exceptions thrown in background threads
Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler(new Handler()));


while (true) {
try {
Looper.loop();
Thread.setDefaultUncaughtExceptionHandler(systemUncaughtHandler);
throw new RuntimeException("Main thread loop unexpectedly exited");
} catch (Throwable e) {
showCrashDisplayActivity(e);
}
}
}

As you can see the uncaught exception handler is used only for the exceptions thrown in background threads. The following handler catches those exceptions and propagates them to the UI thread:

static class UncaughtHandler implements UncaughtExceptionHandler {


private final Handler mHandler;


UncaughtHandler(Handler handler) {
mHandler = handler;
}


public void uncaughtException(Thread thread, final Throwable e) {
mHandler.post(new Runnable() {
public void run() {
throw new BackgroundException(e);
}
});
}
}

An example project which uses this technique is available on my GitHub repo: https://github.com/idolon-github/android-crash-catcher