Android统一异常处理,Android全局异常处理UncaughtExceptionHandler

开发中有些地方未注意可能造成异常抛出未能caught到,然后弹出系统对话框强制退出。这种交互不好,而且开发者也不能及时获取到底哪里出问题,当然可以接入第三方Crash分析平台比如Bugly、instabug,其实也可以自己很简单的实现全局异常处理。

未捕获异常大多发生在在多线程环境中,子线程抛出的异常是不能用主线程中try….catch捕获。可以给线程设置UncaughtExceptionHandler,当出现异常时会回调UncaughtExceptionHandler中的uncaughtException(Thread t, Throwable e) 方法。设置可以为某个线程单独设

thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

当想给所有线程设置时使用

Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());

实现自己的UncaughtExceptionHandler

public class CrashHandler implements Thread.UncaughtExceptionHandler {

private static final String TAG = "CrashHandler";

private Context mContext;

volatile private static CrashHandler sCrashHandler;

private CrashHandler(Context context) {

mContext = context;

}

public static CrashHandler getInstance(Context context){

if (sCrashHandler == null) {

synchronized (CrashHandler.class){

if (sCrashHandler == null) {

//使用Application Context

sCrashHandler=new CrashHandler(context.getApplicationContext());

}

}

}

return sCrashHandler;

}

@Override

public void uncaughtException(Thread t, Throwable e) {

if (BuildConfig.DEBUG) Log.d(TAG, "uncaughtException: "+t.getName()+" "+e.getMessage());

//这里可以将异常信息保存或上传

//...

//可根据情况选择是否干掉当前的进程

android.os.Process.killProcess(android.os.Process.myPid());

}

}

这里自定义的CrashHandler 构造时传入并保存context,以便之后进行异常处理操作,因为使用的单例模式,内部有静态实例,为了防止context引用Activity造成内存泄露,因此使用application context。

在Application中注册未捕获异常处理器

public class CrashApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

Thread.setDefaultUncaughtExceptionHandler(CrashHandler.getInstance(this));

}

}

最后manifest中使用CrashApplication

android:name=".CrashApplication"

android:allowBackup="true"

...