替换默认的未捕获异常处理程序以避免崩溃对话框 [英] Replacing Default Uncaught Exception Handler To Avoid Crash Dialog

查看:110
本文介绍了替换默认的未捕获异常处理程序以避免崩溃对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想替换默认的未捕获异常,以便不显示默认的崩溃对话框.

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

问题是,如果您调用Thread.setDefaultUncaughtExceptionHandler(YourHandler),则在出现异常的情况下,应用程序冻结",并且会出现ANR(应用程序无响应)对话框.我们做了System.exit()Process.killProcess()的实验,它们解决了这个问题,但是从阅读此问题来看,似乎不建议这样做.

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.

那么如何正确完成呢?

推荐答案

TL; DR

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.

首先,很明显,在应用程序崩溃的情况下,必须强制使用System.exit()Process.killProcess()(至少这是Google员工的想法).
重要的是要注意,com.android.internal.os.RuntimeInit.UncaughtHandler可能(并且确实)在框架版本之间进行更改,并且其中的某些代码对于您自己的实现也不可用.
如果您不关心默认崩溃对话框,而只想向默认处理程序中添加一些内容,则应该包装默认处理程序. (例如,请参见底部)

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)

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() + "\n" + "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);
    }
}

})

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

这篇关于替换默认的未捕获异常处理程序以避免崩溃对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆