我知道我可以将以下代码添加到onCreate of Activity来处理uncaughtException。 但在我的BroadcastReceiver应用程序中,我没有UI,也没有Activity,我该怎么办?
final UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//TODO Handle Exception
defaultUEH.uncaughtException(thread, ex);
}
});
答案 0 :(得分:1)
尝试将此代码放入BroadcastReceiver
的构造函数中public MyBroadcastReceiver() {
final UncaughtExceptionHandler defaultUEH = Thread
.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Handle Exception
defaultUEH.uncaughtException(thread, ex);
}
});
}