标签:imp erro 就会 自己 无限循环 class tin data- 进入
public class ThreadHandler implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler mDefaultHandler;
private boolean isInit = false;
/**
* CrashHandler实例
*/
private static ThreadHandler INSTANCE;
/**
* 获取CrashHandler实例 ,单例模式
*/
public static ThreadHandler getInstance() {
if (INSTANCE == null) {
synchronized (CrashHandler.class) {
if (INSTANCE == null) {
INSTANCE = new ThreadHandler();
}
}
}
return INSTANCE;
}
/**
* 当UncaughtException发生时会转入该函数来处理
* 该方法来实现对运行时线程进行异常处理
*/
@Override
public void uncaughtException(Thread t, Throwable e) {
if (mDefaultHandler != null) {
//收集完信息后,交给系统自己处理崩溃
//uncaughtException (Thread t, Throwable e) 是一个抽象方法
//当给定的线程因为发生了未捕获的异常而导致终止时将通过该方法将线程对象和异常对象传递进来。
mDefaultHandler.uncaughtException(t, e);
} else {
//否则自己处理
}
}
/**
* 初始化,注册Context对象,
* 获取系统默认的UncaughtException处理器,
* 设置该CrashHandler为程序的默认处理器
* @param ctx
*/
public void init(Application ctx) {
if (isInit){
return;
}
//获取系统默认的UncaughtExceptionHandler
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//将当前实例设为系统默认的异常处理器
//设置一个处理者当一个线程突然因为一个未捕获的异常而终止时将自动被调用。
//未捕获的异常处理的控制第一个被当前线程处理,如果该线程没有捕获并处理该异常,其将被线程的ThreadGroup对象处理,最后被默认的未捕获异常处理器处理。
Thread.setDefaultUncaughtExceptionHandler(this);
isInit = true;
}
}
public final void dispatchUncaughtException(Throwable e) {
Thread.UncaughtExceptionHandler initialUeh = Thread.getUncaughtExceptionPreHandler();
if (initialUeh != null) {
try {
initialUeh.uncaughtException(this, e);
} catch (RuntimeException | Error ignored) {
// Throwables thrown by the initial handler are ignored
}
}
getUncaughtExceptionHandler().uncaughtException(this, e);
}
public static UncaughtExceptionHandler getUncaughtExceptionPreHandler() {
return uncaughtExceptionPreHandler;
}
// null unless explicitly set
private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
// null unless explicitly set
private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
private ThreadGroup group;
//可以发现ThreadGroup类是集成Thread.UncaughtExceptionHandler接口的
class ThreadGroup implements Thread.UncaughtExceptionHandler{}
public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread \""
+ t.getName() + "\" ");
e.printStackTrace(System.err);
}
}
}
Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));
private static class KillApplicationHandler implements Thread.UncaughtExceptionHandler {
private final LoggingHandler mLoggingHandler;
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
if (mCrashing) return;
mCrashing = true;
if (ActivityThread.currentActivityThread() != null) {
ActivityThread.currentActivityThread().stopProfiling();
}
// Bring up crash dialog, wait for it to be dismissed
ActivityManager.getService().handleApplicationCrash(
mApplicationObject, new ApplicationErrorReport.ParcelableCrashInfo(e));
} catch (Throwable t2) {
if (t2 instanceof DeadObjectException) {
// System process is dead; ignore
} else {
try {
Clog_e(TAG, "Error reporting crash", t2);
} catch (Throwable t3) {
// Even Clog_e() fails! Oh well.
}
}
} finally {
// Try everything to make sure this process goes away.
Process.killProcess(Process.myPid());
System.exit(10);
}
}
}
@FunctionalInterface
public interface UncaughtExceptionHandler {
void uncaughtException(Thread t, Throwable e);
}
03.Android崩溃Crash库之ExceptionHandler分析
标签:imp erro 就会 自己 无限循环 class tin data- 进入
原文地址:https://www.cnblogs.com/yc211/p/13627443.html