1,手动对UI主线程进行 try catch
原理:整个Android系统其实就是运行在UI主线程上,所以如果可以保证主线程不崩溃,也就可以保证app不崩溃,而主线程其实就是Looper.loop()这个方法在维护,执行,所以我们只需要对这个方法进行try catch就可以保证整个UI线程是不会崩溃闪退的。
方法如下:
public class APPextends Applicationimplements IExceptionHandler {
private static APPinstance;
private static MyDataBasemyDataBase;
private static MyDataBasemyDataBase2;
public static APPgetInstance() {
return instance;
}
public static MyDataBasegetMyDataBase() {
return myDataBase;
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 这里拦截所有异常不让App崩溃的解决方法
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
while (true) {
try {
Looper.loop();//try-catch主线程的所有异常;Looper.loop()内部是一个死循环,出现异常时才会退出,所以这里使用while(true)。
}catch (Throwable e) {
Log.d(TAG, "Looper.loop(): " + e.getMessage());
}
}
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {//try-catch子线程的所有异常。
Log.d(TAG, "UncaughtExceptionHandler: " + e.getMessage());
}
});
}
}
}
2,使用第三方库 DefenseCrash
链接地址 https://github.com/xuuhaoo/DefenseCrash
方法一:手动实现IExceptionHandler
public class MyApp extends Application implements IExceptionHandler {
@Override protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// step1: Initialize the lib. DefenseCrash.initialize();
// setp2: Install the fire wall defense. DefenseCrash.install(this);
}
@Override public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
// step3: Print the error when crashed during runtime. throwable.printStackTrace();
// step4: Upload this throwable to your crash collection sdk.
}
@Override public void onEnterSafeMode() {
// We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
}
@Override public void onMayBeBlackScreen(Throwable throwable) {
// onLayout(),onMeasure() or onDraw() has breaks down,
// it causes the drawing to be abnormal and the choreographer to break down.
// We will notify you on this method,you’d better finish this activity or restart the application.
}
}
方法二:继承DefenseCrashApplication
public class MyApp extends DefenseCrashApplication {
@OverridepublicvoidonCaughtException(Threadthread,Throwablethrowable,booleanisSafeMode) {
//step1: Print the error when crashed during runtime.throwable.printStackTrace();
//step2: Upload this throwable to your crash collection sdk.
}
@OverridepublicvoidonEnterSafeMode() {
//We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
}
@OverridepublicvoidonMayBeBlackScreen(Throwablethrowable) {
//onLayout(),onMeasure() or onDraw() has breaks down,
//it causes the drawing to be abnormal and the choreographer to break down.
//We will notify you on this method,you’d better finish this activity or restart the application.
}
}
3,使用第三方库 CockRoach
链接地址 https://github.com/android-notes/Cockroach
思路:先全局捕获,再分发处理
publicclassAppextendsApplication{
@OverridepublicvoidonCreate() {
super.onCreate(); install(); }privatevoidinstall() {
Cockroach.install(newExceptionHandler() {
@OverrideprotectedvoidonUncaughtExceptionHappened(Threadthread,Throwablethrowable) {
Log.e("AndroidRuntime","--->onUncaughtExceptionHappened:"+thread+"<---", throwable);
newHandler(Looper.getMainLooper()).post(newRunnable() {
@Overridepublicvoidrun() {
toast.setText(R.string.safe_mode_excep_tips);
toast.show();
}
});
}
@OverrideprotectedvoidonBandageExceptionHappened(Throwablethrowable) {
throwable.printStackTrace();
//打印警告级别log,该throwable可能是最开始的bug导致的,无需关心
toast.setText("Cockroach Worked"); toast.show();
}
@OverrideprotectedvoidonEnterSafeMode() {
inttips=R.string.safe_mode_tips;Toast.makeText(App.this, getResources().getString(tips),Toast.LENGTH_LONG).show();
}
@OverrideprotectedvoidonMayBeBlackScreen(Throwablee) {
Threadthread=Looper.getMainLooper().getThread();Log.e("AndroidRuntime","--->onUncaughtExceptionHappened:"+thread+"<---", e);
//黑屏时建议直接杀死
appsysExcepHandler.uncaughtException(thread,newRuntimeException("black screen"));
}
});
}
}
网友评论