Android开发经验谈程序员半栈工程师

如何优雅的避免android(安卓)运行时崩溃

2018-08-16  本文已影响435人  xuuhaoo

一.问题抛出

二.解决效果

预防崩溃演示效果图.gif

三.解决思路

四.成品Library

Github项目地址: https://github.com/xuuhaoo/DefenseCrash (欢迎Star)
集成方法:

allprojects {
    repositories {
        maven { url 'https://dl.bintray.com/xuuhaoo/maven/'}
    }
}
dependencies {
    compile 'com.tonystark.android:defense_crash:2.0.0'
}

We provide you two options for choosing:

public class MyApp extends Application implements IExceptionHandler {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    // step1: Initialize the lib.
        DefenseCrash.initialize();
    // setp2: Install the fire wall defense.
        DefenseCrash.install(this);
    }

    @Override
    public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
    // step3: Print the error when crashed during runtime.
        throwable.printStackTrace();
    // step4: Upload this throwable to your crash collection sdk.
    }

    @Override
    public void onEnterSafeMode() {
    // We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
    }

    @Override
    public void onMayBeBlackScreen(Throwable throwable) {
    // onLayout(),onMeasure() or onDraw() has breaks down,
    // it causes the drawing to be abnormal and the choreographer to break down.
    // We will notify you on this method,you’d better finish this activity or restart the application.
    }
}
public class MyApp extends DefenseCrashApplication {
    @Override
    public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
    // step1: Print the error when crashed during runtime.
        throwable.printStackTrace();
    // step2: Upload this throwable to your crash collection sdk.
    }

    @Override
    public void onEnterSafeMode() {
    // We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
    }

    @Override
    public void onMayBeBlackScreen(Throwable throwable) {
    // onLayout(),onMeasure() or onDraw() has breaks down,
    // it causes the drawing to be abnormal and the choreographer to break down.
    // We will notify you on this method,you’d better finish this activity or restart the application.
    }
}

It is Done!

上一篇 下一篇

猜你喜欢

热点阅读