【发布时间】: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