前言
在app开发中,难免会出现一些预料之外的问题导致app crash,如果应用不能自动重启,会大大影响用户的体验。比如聊天软件在后台接受消息的服务需要一直存在,如果crash后不能及时恢复,那么就会错过很多消息。又比如普通天气应用需要定时去获取天气,如果crash后不能及时恢复,用户对天气的了解将滞后,从而可能影响用户的出行。
针对上面的问题,提高app稳定性是最主要的,但我们仍需要留一手,确保第一层防线被攻破后我们能够兜底。
相关知识补充
Java中有两种异常:已检测异常(Checked exceptions)和未检测异常(Unchecked exceptions)。前者必须使用 throws 或 try catch 进行异常处理,例如Thread.sleep()
或文件读写;后者不需要指定或捕获,例如数组越界异常和空指针异常。
java对未检测异常默认处理方式是:将堆栈跟踪信息写到控制台中(或者记录到错误日志文件中)然后退出程序。
当线程由于未捕获的异常而即将终止时,Java 虚拟机将使用 Thread.getUncaughtExceptionHandler()
查询线程的 UncaughtExceptionHandler
,并将调用处理程序的 uncaughtException
方法,将线程和异常作为参数传递。
如果线程尚未显式设置其 UncaughtExceptionHandler
,则其 ThreadGroup
对象将充当其 UncaughtExceptionHandler
。如果 ThreadGroup
对象对处理异常没有特殊要求,它可以将调用转发到默认的未捕获异常处理程序。
实现
基于上述原理,我们可以可以构建一个类,继承java的UncaughtExceptionHandler
接口,并覆写uncaughtException
方法,从而实现处理Crash问题并重启应用。
public class UnCeHandler implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler mDefaultHandler;
Application application;
public UnCeHandler(Application application) {
//获取系统默认的UncaughtException处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
this.application = application;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
//....
}
}
在uncaughtException(Thread thread, Throwable ex)
中,我们可以加入自定义的异常处理
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
//设置定时重启
Intent intent = new Intent(application.getApplicationContext(), MainActivity.class);
PendingIntent restartIntent = PendingIntent.getActivity(
application.getApplicationContext(),
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
//kill应用
android.os.Process.killProcess(android.os.Process.myPid());
}
}
其中handleException(ex)
可以用来收集crash信息,并进行文件存储或弹出Toast等操作
private boolean handleException(Throwable ex) {
if (ex == null) {
return false;
}
//TODO:在此处处理捕获到的crash信息
return true;
}
程序中的AlarmManager是android中的定时器,通过传递PendingIntent对象,可以定时启动指定的actcivity、service或broadcast,从而实现app被kill后定时重启。
注意:对于后台服务的崩溃重启,8.0以上需使用PendingIntent.getForegroundService()
来唤醒,同时要唤醒的后台服务必须以前台的形式启动
PendingIntent restartIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
restartIntent = PendingIntent.getForegroundService(
application.getApplicationContext(), 0, intent, 0);
} else {
restartIntent = PendingIntent.getService(
application.getApplicationContext(), 0, intent, 0);
}
AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
最后,在application中设置自定义的异常捕获处理器,就能实现捕获全局所有线程的异常
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//程序崩溃时触发线程 以下用来捕获程序崩溃异常
Thread.setDefaultUncaughtExceptionHandler(new UnCeHandler(this));
}
}
总结
应用异常奔溃后重启的思路是:通过Thread.UncaughtExceptionHandler
捕获app crash的异常,然后通过AlarmManager
发送定时广播,重新打开应用或后台服务。