1.自定义类实现 UncaughtExceptionHandler ,重写 uncaughtException。
import java.lang.Thread.UncaughtExceptionHandler;
public class myCustomExceptionHandler implements UncaughtExceptionHandler{
private UncaughtExceptionHandler defaultUEH;
public myCustomExceptionHandler() {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
System.out.println("应用程序异常");
/**
* 处理异常,保存异常log或向服务器发送异常报告
*/
defaultUEH.uncaughtException(thread, ex);;
}
}
2.在要调用的activity 中(onCreate)加入
Thread.setDefaultUncaughtExceptionHandler(new myCustomExceptionHandler());