CustomExceptionHandler是,setDefaultUncaughtExceptionHandler使應用程序崩潰默默
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
,我將獲得默認行爲。
是否有任何方法來實現我的班級中的默認錯誤行爲?
在此先感謝!
我有同樣的問題,最近解決了。這是我的[原文](https://stackoverflow.com/questions/46070393/replacing-default-uncaught-exception-handler-to-avoid-crash-dialog) –