1、实现UncaughtExceptionHandler接口
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{
private static MyUncaughtExceptionHandler myUncaughtExceptionHandler = null;
private MyUncaughtExceptionHandler(){
}
public static MyUncaughtExceptionHandler getInstance(){
if (myUncaughtExceptionHandler == null){
synchronized (MyUncaughtExceptionHandler.class){
if (myUncaughtExceptionHandler == null){
myUncaughtExceptionHandler = new MyUncaughtExceptionHandler();
}
}
}
return myUncaughtExceptionHandler;
}
public void setDefaultUnCachExceptionHandler(){ //在Application开始时调用
Thread.setDefaultUncaughtExceptionHandler(this); //设置应用默认的全局捕获异常器
}
@Override /
public void uncaughtException(Thread thread, Throwable throwable) {
//异常信息,上传错误日志
}
}
2、注册UncaughtExceptionHandler接口
public class MyAplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//设置全局捕获异常器
MyUncaughtExceptionHandler.getInstance().setDefaultUnCachExceptionHandler();
}
}