猿猿们都知道日常码bug都会使用 try - catch拦截异常,主动try-catch是可以预防异常闪退。但是本着“辛辛苦苦敲的bug凭什么要改!”的宗旨,日常闪退该怎么办,猿猿们别慌,一招捕捉全局crash,也可用作埋点cras分析或者缓存io到file作为log,据我所知像友盟这些第三方sdk就嵌套了crash分析,长话短说,直接上码吧
public class CrashCatchHandler implements UncaughtExceptionHandler {
public static final String TAG = "CrashCatchHandler";
private static CrashCatchHandler crashHandler = new CrashCatchHandler();
private Context mContext;
private UncaughtExceptionHandler mDefaultCaughtExceptionHandler;
/**
* 饿汉单例模式(静态)
*/
public static CrashCatchHandler getInstance() {
return crashHandler;
}
public void init(Context context) {
mContext = context;
//获取默认的系统异常捕获器
mDefaultCaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
//把当前的crash捕获器设置成默认的crash捕获器
Thread.setDefaultUncaughtExceptionHandler(this);
}