腾讯 Bugly 和 CrashHandler 冲突,不上传日志

简单介绍

CrashHandler 是继承 UncaughtExceptionHandler 类来处理 app 崩溃,自由度比较大
可以收集日志信息保存到本地,上传网络,并重启应用.可以说是除了三方的异常上报工具,
开发者使用最多的一种方式

Bugly 是腾讯公司为移动开发者开放的服务之一,这里主要指 Crash 监控、崩溃分析等质量跟踪服务。


问题描述

CrashHandler 是 app 以前就有的错误收集机制,但是其日志功能并不是很强大.因此引入 Bugly 的
异常上报功能.但是集成好之后 调用CrashReport.testJavaCrash(); 测试
一直不上传日志到控制台


解决办法

解决办法在初始化顺序

//异常日志捕获
CrashHandler.getInstance().init(this);
//腾讯bugly,需放在 CrashHandler 后面,否则无法上传信息
initBugly();


private void initBugly() {
    CrashReport.initCrashReport(getApplicationContext(), "appid", true);
}

问题分析

这里对问题产生原因做一些探究
首先我们把问题还原

 initBugly();
 //异常日志捕获
 CrashHandler.getInstance().init(this);

这样运行后 调用CrashReport.testJavaCrash(); 测试, 结果日志不上传

接下来看 CrashHandler 的 init() 方法,

public void init(Context context) {
        mContext = context;
        mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        //设置当前的处理类为默认异常处理器
       // Thread.setDefaultUncaughtExceptionHandler(this);
}

我们把最后一行注释掉,结果日志能正常上传

这里有产生 第一个猜想 :是这个设置默认异常捕获类的代码引起的. 当然实际上我们不能注释,
否则 CrashHandler 就作废了

接下来 第二个猜想 :是不是在实际异常处理中导致的 Bugly 失效呢?


 //取消注释
 Thread.setDefaultUncaughtExceptionHandler(this);

//注释掉所有异常处理方法
 @Override
    public void uncaughtException(Thread thread, Throwable ex) {
//        boolean isHandle = handleException(ex);
//        if (!isHandle) {
//            if (mDefaultUncaughtExceptionHandler != null) {
//                //如果程序员没有处理异常,则交给系统的异常处理器
//                mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
//            } else {
//                //如果默认异常处理器为空,则需要退出程序。
//                exitSystem();
//            }
//        }
    }

运行程序测试,结果日志不上传


到这里基本确定产生冲突的原因是

Thread.setDefaultUncaughtExceptionHandler(this);

但是这个方法的源码

public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler handler) {
        Thread.defaultUncaughtHandler = handler;
}

以及 UncaughtExceptionHandler 源码

 /**
     * Implemented by objects that want to handle cases where a thread is being
     * terminated by an uncaught exception. Upon such termination, the handler
     * is notified of the terminating thread and causal exception. If there is
     * no explicit handler set then the thread's group is the default handler.
     */
    public static interface UncaughtExceptionHandler {
        /**
         * The thread is being terminated by an uncaught exception. Further
         * exceptions thrown in this method are prevent the remainder of the
         * method from executing, but are otherwise ignored.
         *
         * @param thread the thread that has an uncaught exception
         * @param ex the exception that was thrown
         */
        void uncaughtException(Thread thread, Throwable ex);
    }

都未能给予我更深入探索的入口,到这里也只能停止了.
如果哪位小伙伴有新的分析思路还望能留言在此,谢谢.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值