1. 創(chuàng)建MyApplication extends MultiDexApplication
在oncreate初始化
//bug監(jiān)聽操作
Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
2. 創(chuàng)建異常捕獲類
private class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
//當(dāng)有未捕獲的bug的時(shí)候調(diào)用的方法
//ex : 異常
@Override
public void uncaughtException(Thread thread, Throwable ex) {
System.out.println("哥發(fā)現(xiàn)異常了,哥捕獲了....");
//上傳error代碼
MobclickAgent.reportError(MyApplication.this, ex);
try {
//將異常信息保存到本地文件中
ex.printStackTrace(new PrintStream(new File("mnt/sdcard/error.log")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//自殺
//閃退操作
android.os.Process.killProcess(android.os.Process.myPid());
}