Android crash 默认处理流程

当Android应用发生crash时,系统会显示Force close Dialog。此过程涉及Thread.UncaughtExceptionHandler,其默认行为包括打印异常信息和调用AMS处理。可以通过自定义UncaughtExceptionHandler提供更友好的用户反馈,通常在Application.onCreate()中设置。
摘要由CSDN通过智能技术生成

Android APP crash 的时候会弹出一个 Force close Dialog,这篇文章主要记录一下该 Dialog 的显示流程,以及由此引申出来的自定义 APP crash 处理流程

  1. Force close Dialog 的显示流程
  2. 自定义 Thread.UncaughtExceptionHandler 处理 APP crash

Force close 处理流程

Android 主要使用Thread.UncaughtExceptionHandler 机制来处理 APP Crash,这个defaultUncaughtException 定义在 RuntimeInit.commonInit()

private static final void commonInit() {
    ......

    /* set default handler; this applies to all threads in the VM */
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtHandler());

    ......
}

private static class UncaughtHandler implements Thread.UncaughtExceptionHandler {
   
    public void uncaughtException(Thread t, Throwable e) {
        try {
            ......

            if (mApplicationObject == null) {
                Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e);
            } else {
                StringBuilder message = new StringBuilder();
                // 1) 把Exception信息打印到logcat中
                message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n");
                final String processName = ActivityThread.currentProcessName();
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值