1. 创建MyApplication extends MultiDexApplication
在oncreate初始化
//bug监听操作
Thread.currentThread().setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
2. 创建异常捕获类
private class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
//当有未捕获的bug的时候调用的方法
//ex : 异常
@Override
public void uncaughtException(Thread thread, Throwable ex) {
System.out.println("哥发现异常了,哥捕获了....");
//上传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());
}