【问题标题】:Report uncaught exceptions报告未捕获的异常
【发布时间】:2013-07-25 00:17:39
【问题描述】:

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

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

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

【问题讨论】:

  • 就个人而言,我会使用 ACRA 作为起点,自定义它以与我的后端系统对话(或将后端系统调整为 ACRA 就绪)。
  • 忽略我之前的评论,没有意识到你在安卓上。但是,您应该始终更喜欢 ExecutorService 而不是自己创建新的 Threads
  • @CommonsWare 感谢您的建议。
  • @ColinMorelli 创建一个简单的线程对我来说就足够了,不需要管理线程池或ExecutorSerice 提供的任何东西。

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


【解决方案1】:

不要自己做,而是使用已经设置好的服务来做。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多