42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
package com.xscm.moduleutil.utils;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
|
|
import com.alibaba.android.arouter.launcher.ARouter;
|
|
|
|
public class CrashHandler implements Thread.UncaughtExceptionHandler {
|
|
private static CrashHandler instance;
|
|
private Thread.UncaughtExceptionHandler defaultHandler;
|
|
|
|
private CrashHandler(Context context) {
|
|
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
|
|
}
|
|
|
|
public static void init(Context context) {
|
|
if (instance == null) {
|
|
instance = new CrashHandler(context);
|
|
Thread.setDefaultUncaughtExceptionHandler(instance);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void uncaughtException(Thread t, Throwable e) {
|
|
// 记录崩溃日志
|
|
Log.e("CrashHandler", "未捕获异常: " + e.getMessage());
|
|
// 简单处理空指针
|
|
if (e instanceof NullPointerException) {
|
|
// 重启应用或跳转错误页
|
|
restartApp();
|
|
} else {
|
|
// 交给系统默认处理
|
|
defaultHandler.uncaughtException(t, e);
|
|
}
|
|
}
|
|
|
|
private void restartApp() {
|
|
// 实现应用重启逻辑
|
|
ARouter.getInstance().build(ARouteConstants.ME).navigation();
|
|
}
|
|
}
|