全局捕获异常类

来源:互联网 发布:三菱plc手持编程器 编辑:程序博客网 时间:2024/06/11 12:03

//捕获异常类

import android.util.Log;

 

public class ExecptionHandler implements Thread.UncaughtExceptionHandler {

    private static ExecptionHandlerexecptionHandler = null;

 

    private ExecptionHandler() {

 

    }

    //这个方法是要在application里面调用的,分为一级一级,一级一级调

    public static ExecptionHandlergetInstance(){

        if (execptionHandler ==null){

            synchronized(ExecptionHandler.class){

                if (execptionHandler== null){

                    execptionHandler= new ExecptionHandler();

                }

            }

        }

        return execptionHandler;

    }

 

    //在application调用,第三级

    public voidsetDefaultUnCachExceptionHandler(){

        //设置应用默认的全局异常捕获器

        Thread.setDefaultUncaughtExceptionHandler(this);

    }

 

    @Override

    public voiduncaughtException(Thread thread, Throwable throwable) {

       Log.d("myTag","程序出现异常"+throwable);

    }

}

Application类

import android.app.Application;

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ExecptionHandler.getInstance().setDefaultUnCachExceptionHandler();
    }
}

原创粉丝点击