Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

android - Replacing Default Uncaught Exception Handler To Avoid Crash Dialog

We wanted to replace the default uncaught exception so that the default crash dialog is not shown.

The problem was that if you call Thread.setDefaultUncaughtExceptionHandler(YourHandler) then in case of an exception the app "freezes" and you get an ANR (application not responding) dialog. We did experiment with System.exit() and Process.killProcess() which solved the issue but from reading on the matter it seemed that this is discouraged.

So how can it be done correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TL;DR

Adopt the code in the framework's implementation of the default uncaught exception handler in com.android.internal.os.RuntimeInit.UncaughtHandler omitting the part that shows the dialog.

Drilldown

First it is clear that System.exit() and Process.killProcess() are mandatory in the scenario where the app is crashing (at least that's how the folks in Google think).
It is important to note that com.android.internal.os.RuntimeInit.UncaughtHandler may (and does) change between framework releases, also some code in it is not available for your own implementation.
If you are not concerned with default crash dialog and just want to add something to the default handler you should wrap the default handler. (see bottom for example)

Our Default Uncaught Exception Handler (sans dialog)

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable ex) {
    try {
        // Don't re-enter -- avoid infinite loops if crash-reporting crashes.
        if (mCrashing) {
            return;
        }
        mCrashing = true;

        String message = "FATAL EXCEPTION: " + t.getName() + "
" + "PID: " + Process.myPid();
        Log.e(TAG, message, ex);
    } catch (Throwable t2) {
        if (t2 instanceof DeadObjectException) {
            // System process is dead; ignore
        }
        else {
            try {
                Log.e(TAG, "Error reporting crash", t2);
            } catch (Throwable t3) {
                // Even Log.e() fails!  Oh well.
            }
        }
    } finally {
        // Try everything to make sure this process goes away.
        Process.killProcess(Process.myPid());
        System.exit(10);
    }
}

})

Wrapping the default handler

final Thread.UncaughtExceptionHandler defHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable ex) {
        try {
            //your own addition 
        } 
        finally {
            defHandler.uncaughtException(t, ex);
        }
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...