How to use ACRA and also set UncaughtExceptionHand

2019-06-01 19:30发布

问题:

In my application, i want to set the uncaught exception handler so that i can do stuff in the event that of an unforseen crash. (i want to do stuff like close out sockets, clear notification...etc.)

Thread.currentThread().setDefaultUncaughtExceptionHandler(sDefaultThreadHandler);

where

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);
    }
};

the problem is, i want ACRA to also report before the process exits. how do i accomplish this?

回答1:

Oh wait, nvm. i found out that ACRA uses the default exception handler (according to https://github.com/ACRA/acra/blob/master/src/main/java/org/acra/ErrorReporter.java#L201), so that means that if your own thread has a thread exception handler, that will be used first.

Thread.currentThread().setUncaughtExceptionHandler(mYourOwnThreadHandler);

and if you really need to use ACRA, then inside the uncaughtException() method that you override, just delegate it upwards by calling Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, ex);