Android系统的“程序异常退出”,给应用的用户体验造成不良影响。为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理。通过Thread.setDefaultUncaughtExceptionHandler()方法将异常处理类设置到线程上即可。重写uncaughtException方法,注意只有程序未捕获的异常才会调用这里,如果程序捕获了,就不会了。
package demo.lbb.anotest;
import android.content.Context;
import java.lang.Thread.UncaughtExceptionHandler;
/**
* Created by liaobinbin on 2015/12/13.
*/
public class CrashHandler implements UncaughtExceptionHandler {
private static CrashHandler INSTANCE = new CrashHandler();
private Context mContext;
private UncaughtExceptionHandler mDefaultHandler;
private CrashHandler() {
}
public static CrashHandler getInstance() {
return INSTANCE;
}
public void init(Context ctx) {
mContext = ctx.getApplicationContext();//防止activity释放不了,导致OOM
//mContext = ctx; 如果想显示dialog的话,就不能用ApplicationContext了
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); //获取系统默认的异常处理器
//Sets the default uncaught exception handler. This handler is invoked in case any Thread dies due to an unhandled exception.只捕获为处理的异常,回调uncaughtException方法
//设置异常处理器为this,注意要在getDefaultUncaughtExceptionHandler后调用,否则前面的结果mDefaultHandler就是this了,那么uncaughtException无限循环调用
Thread.setDefaultUncaughtExceptionHandler(this);
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
QDLog.d("uncaughtException");
handleException(ex);
//系统提供了默认的异常处理器,则系统结束程序,否则自己结束自己,实际测试mDefaultHandler不为null的,所以还是会出现“停止运行”框的,系统默认行为
if (mDefaultHandler != null) {
QDLog.d("mDefaultHandler is not null");
mDefaultHandler.uncaughtException(thread, ex);
} else {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑
*
* @param ex
* @return true:如果处理了该异常信息;否则返回false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return true;
}
QDLog.d("handleException: " + ex);
//1. 可以把ex存储在存储卡中
//2. 实际开发更多的把crash信息上报给服务器,供开发人员分析,不只记录crash信息,同时还记录手机的机型,型号等方便开发人员复现
//3. 这里还可以弹出一个dialog,提醒用户,发生了异常
return true;
}
}
public class BaseApp extends Application {
@Override
public void onCreate() {
super.onCreate();
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this);
}
}
//Activity中模拟一下
@OnClick(R.id.but)
void onClick() {
throw new RuntimeException("自定义异常,同时程序未捕获");
}
实际结果:
D/LiaBin: uncaughtException
D/LiaBin: handleException: java.lang.RuntimeException: 自定义异常,同时程序未捕获
D/LiaBin: mDefaultHandler is not null
出现“停止运行”框的
注释该行:
if (mDefaultHandler != null) {
QDLog.d("mDefaultHandler is not null");
mDefaultHandler.uncaughtException(thread, ex);
} else {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
那么是不会出现出现“停止运行”框,因为未捕获的异常被我们自己处理了,不经过系统。所以不出现“停止运行”框
一开始以为子线程发生异常是没法捕获的
@OnClick(R.id.but)
void onClick() {
new Thread() {
@Override
public void run() {
throw new RuntimeException("自定义异常,同时程序未捕获");
}
}.start();
}
后来测试了一下,也是可以捕获的,因为
Thread.setDefaultUncaughtExceptionHandler(this);
为该进程设置了默认的未捕获异常处理器,注意是进程,而不是该线程,因为defaultUncaughtHandler
是静态属性的。。。
/**
* Holds the default handler for uncaught exceptions, in case there is one.
*/
private static UncaughtExceptionHandler defaultUncaughtHandler;
public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler handler) {
Thread.defaultUncaughtHandler = handler;
}
defaultUncaughtHandler是静态属性,全局的,所以设置了一次,所有线程包括子线程,main线程的未捕获的异常都会被UncaughtExceptionHandler处理的