抓取Crash不让崩溃

主动抓取crash,并处理下一个消息

去掉ActivityThread类中处理消息的方法,避免黑屏

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {

        while (true) {
            try {
                Looper.loop();
            } catch (Exception e) {
                if (e instanceof QuitException) { //退出
                    return;
                }
                long realTime = SystemClock.elapsedRealtime();
                if (mTime == 0) {
                    mTime = realTime;
                } else {
                    if (realTime - mTime < 1000) {
                        mProxyCrashHandler.uncaughtException(Thread.currentThread(), e);
                    } else {
                        mTime = realTime;
                    }
                }

                StackTraceElement[] stackTraceElement = e.getStackTrace();
                for (StackTraceElement element : stackTraceElement) {
                    //目前先过滤这部分,后续可以增加系统方面的,业务方面的到时候开接口出去
                    if ("ActivityThread.java".equals(element.getFileName()) && "handleMessage".equals(element.getMethodName())) {
                        mProxyCrashHandler.uncaughtException(Thread.currentThread(), e);
                        return;
                    }
                }
                sendExceptionLog(e, ACTIVE_DEFENSE);
            }
        }
    }
});

猜你喜欢

转载自my.oschina.net/u/1013713/blog/1785617