自定义异常处理程序
public class CustomExceptionHandler implements UncaughtExceptionHandler {
private Context ctx;
private ContentResolver cr;
public CustomExceptionHandler(Context ctx, ContentResolver cr) {
this.ctx = ctx;
this.cr = cr;
}
public void uncaughtException(Thread t, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
String deviceUuid = Utilities.DeviceUuid(ctx, cr);
String bluetoothName = Utilities.LocalBluetoothName();
AsyncTasks.ErrorLogTask logTask = new AsyncTasks.ErrorLogTask(e, bluetoothName, deviceUuid, stacktrace);
logTask.execute();
}
}
从我的主要活动中调用:
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(getBaseContext(), getContentResolver()));
现在发生异常时,我没有收到常规的“不幸的是,已停止”弹出窗口。它只是一个黑屏。如果我从我的主要活动中删除我的呼叫,换句话说,CustomExceptionHandler
不再使用,我会得到默认行为。
有没有办法在我的班级中实现默认错误行为?
提前致谢!