首页 > 解决方案 > Android/Java:UncaughtExceptionHandler 和 Bugsnag 并行?

问题描述

我正在使用Bugsnag进行自动错误报告,并希望在应用程序崩溃时执行一些额外的操作,例如重新启动。

问题:两者都单独工作,但没有结合起来。一旦我添加MyUncaughtExceptionHandlerBugsnag 就会停止报告。

应用程序

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        Bugsnag.init(this);

        // "Breaks" Bugsnag:
        Thread.setDefaultUncaughtExceptionHandler(
            new MyUncaughtExceptionHandler(this, MainActivity.class));
    }
}

MyUncaughtExceptionHandler

public class MyUncaughtExceptionHandler implements
    java.lang.Thread.UncaughtExceptionHandler {

    private final Context mContext;
    private final Class<?> mActivityClass;

    public MyUncaughtExceptionHandler(Context context, Class<?> c) {
        mContext = context;
        mActivityClass = c;
    }

    public void uncaughtException(Thread thread, Throwable exception) {
        Bugsnag.notify(exception);
        Intent intent = new Intent(mContext, mActivityClass);
        mContext.startActivity(intent); // restarts the app

        Process.killProcess(Process.myPid());
        System.exit(0);
    }
}

我已经尝试Bugsnag.notify(exception)MyUncaughtExceptionHandler.

任何想法?提前致谢!

标签: javaandroidexceptionerror-handlingbugsnag

解决方案


I spotted your post here but do always reach out to us via Bugsnag support if you want a guaranteed response.

The issue is that when you're calling Thread.setUncaughtExceptionHandler, you're removing the handler which Bugsnag sets up here: https://github.com/bugsnag/bugsnag-android/blob/2308eb6c706f66495dea116acf619f695530dff4/sdk/src/main/java/com/bugsnag/android/ExceptionHandler.java#L31

You'd need to call the original handler in order for Bugsnag to detect anything.

Note that killing the process and launching a new intent is not an approach we'd recommend in general in an Android app.


推荐阅读