报告未捕获的异常

时间:2013-07-25 00:17:40

标签: java android uncaught-exception android-anr-dialog

我想抓住未捕获的异常并将有关它们的报告发送到我的服务器。这就是我实现它的方式:

public final class MyApplication extends Application
{
    private static Context context;
    private static Handler mainHandler;
    private static boolean isDebug;

    @Override
    public void onCreate()
    {
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler()
        {
            @Override
            public void uncaughtException(final Thread thread, final Throwable ex)
            {
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        Looper.prepare();

                        ReportUtils.sendReport(thread, ex);
                        Thread.currentThread().getUncaughtExceptionHandler().uncaughtException(thread, ex);
                        Looper.loop();
                        Looper.myLooper().quit();
                    }
                }.start();
            }
        });

        context = getApplicationContext();
        mainHandler = new Handler();
        isDebug = (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
    }

    @Override
    public void onTerminate(){}

    public static Context getAppContext()
    {
        return context;
    }

    public static boolean isDebugMode()
    {
        return isDebug;
    }

    public static Handler getMainHandler()
    {
        return mainHandler;
    }
}

这是实施它的正确方法吗?还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

不要自己动手,而是使用已经设置的服务来完成。

Acra就是其中之一,可以将您的崩溃报告上传到Google文档电子表格。

Bugsense是另一个。它有免费帐户和付费帐户。

Crashlytics对于Android来说相当新,但完全免费,所以我推荐它。设置起来非常简单。

相关问题