最近在项目中集成了Firebase的crash报告插件,遇到了一个小的问题,由于项目中之前也使用的自定义的Thread.UncaughtExceptionHandler(具体实现是重启了app,并屏蔽掉了系统的应用程序停止的弹框),导致覆盖掉了Firebase这个对异常处理的设置.
public interface UncaughtExceptionHandler {
/**
* Method invoked when the given thread terminates due to the
* given uncaught exception.
* <p>Any exception thrown by this method will be ignored by the
* Java Virtual Machine.
* @param t the thread
* @param e the exception
*/
void uncaughtException(Thread t, Throwable e);
}
上面是UncaughtExceptionHandler这个接口的定义,Thread.UncaughtExceptionHandler这个是个什么东西呢,了解的同学可能大致都明白,它是一个由系统收集线程异常并可以被进行处理的一个时机,最终触发uncaughtException这个方法的执行,意味着我们可以自定义设置这个handler,当系统发生异常时,我们可以进行自己的处理.首先分析下它的设值的实现,下面看代码,其实比较好理解.
/**
* Set the default handler invoked when a thread abruptly terminates
* due to an uncaught exception, and no other handler has been defined
* for that thread.
*
*/
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
defaultUncaughtExceptionHandler = eh;
}
/**
* Returns the default handler invoked when a thread abruptly terminates
* due to an uncaught exception. If the returned value is <tt>null</tt>,
*/
public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
return defaultUncaughtExceptionHandler;
}
上面是它在Thread类中的set和get方法,可以看到,defaultUncaughtExceptionHandler是一个静态属性,即说明了它在java程序中是全局唯一的对象.至于get方法,是在哪里被调用,我们就不讨论了.不知道大家是否有印象,每次我们开发的app出错的时候,系统就给我们弹了一个经典的提示,应用程序停止运行,其实它里面也是根据这个UncaughtExceptionHandler来实现的.我