设置Thread.setDefaultUncaughtExceptionHandler是否会阻止Google的错误报告工作?

时间:2012-09-21 18:53:05

标签: android exception-handling

在我的Android应用程序中,我利用setDefaultUncaughtExceptionHandler在用户设备上本地存储有关未处理异常的信息。在一些反馈之后,我怀疑此代码阻止了内置的Google错误报告功能,因为我没有在开发者控制台中看到错误报告,而用户​​报告了异常。他们的设备远远超过2.2,引入了错误报告。可能是具有4.0.3的特定设备不支持此功能吗?如果是,我该如何以编程方式检测到它?

我在Android文档中找不到相关信息。我希望标准错误报告和我的自定义处理一起工作。在我的自定义异常处理程序中,我调用Thread.getDefaultUncaughtExceptionHandler()来获取默认处理程序,在我的uncaughtException实现中,我也将异常传播到此默认处理程序。

2 个答案:

答案 0 :(得分:1)

我首先尝试按this SO answer中的提及调用System.exit(1);,但这不起作用。

最后通过在Androids默认uncaughtException(Thread thread, Throwable ex)上再次调用UncaughtExceptionHandler来解决问题(通过查看ACRA source code找到它。

示例活动

public class MainActivity extends Activity implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler _androidUncaughtExceptionHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _androidUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);

        // Rest onCreate
        setContentView(R.layout.main_activity);
    }

    //@Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            // Do your stuff with the exception
        } catch (Exception e) {
            /* Ignore */
        } finally {
            // Let Android show the default error dialog
            _androidUncaughtExceptionHandler.uncaughtException(thread, ex);
        }
    }
}

答案 1 :(得分:0)

是的,这将停止内置的错误报告。当您的应用崩溃时,用户会收到一个对话框,并可选择通过Google Play报告错误。但是,如果您使用setDefaultUncaughtExceptionHandler(),则会在您的应用中处理该异常,并且不会给出报告该选项的选项。

我建议您将ACRA集成到项目中,因为它可以让您在崩溃时轻松收到错误报告。