继承接口UncaughtExceptionHandler,并重写里面的uncaughtException(Thread thread, Throwable ex)方法,这样就可以监测应用程序的异常情况,做相应的处理:
public class myCustomExceptionHandler implements UncaughtExceptionHandler
private UncaughtExceptionHandler
public myCustomExceptionHandler() {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
System.out.println("应用程序异常");
defaultUEH.uncaughtException(thread, ex);;
}
}
然后在Activity中加入 Thread.setDefaultUncaughtExceptionHandler(new myCustomExceptionHandler());即可。