如何捕获app crash的异常,并重启应用【可用于APP保活】

前言

在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发送定时广播,重新打开应用或后台服务。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值