UncaughtExceptionHandler

来源:互联网 发布:深红累之渊 知乎 编辑:程序博客网 时间:2024/06/05 06:27

继承接口UncaughtExceptionHandler,并重写里面的uncaughtException(Thread thread,Throwable ex)方法,这样就可以监测应用程序的异常情况,做相应的处理:
public class myCustomExceptionHandler implementsUncaughtExceptionHandler {

    privateUncaughtExceptionHandler defaultUEH;

    publicmyCustomExceptionHandler() {

       this.defaultUEH =Thread.getDefaultUncaughtExceptionHandler();
    }

   @Override
    public voiduncaughtException(Thread thread, Throwable ex) {
       // TODOAuto-generated method stub
      System.out.println("应用程序异常");
      
      defaultUEH.uncaughtException(thread, ex);;
    }
    
}
然后在Activity中加入Thread.setDefaultUncaughtExceptionHandler(newmyCustomExceptionHandler());即可。

0 0
原创粉丝点击