Android中捕获全局异常实现代码
程序员文章站
2022-03-23 13:52:56
1、实现uncaughtexceptionhandler,在方法uncaughtexception中处理没有捕获的异常。
public class globale...
1、实现uncaughtexceptionhandler,在方法uncaughtexception中处理没有捕获的异常。
public class globalexception implements uncaughtexceptionhandler { private final static globalexception mycrashhandler = new globalexception(); private globalexception() { } public static synchronized globalexception getinstance() { return mycrashhandler; } public void uncaughtexception(thread arg0, throwable arg1) { trace.log("-------------caught exception--"); } }
2、继承application ,在其中调用thread方法setdefaultuncaughtexceptionhandler,来捕获异常
代码:
public class myapplication extends application { public void oncreate() { super.oncreate(); globalexception handler = globalexception.getinstance(); thread.setdefaultuncaughtexceptionhandler(handler); } }