Android - Crashlytics, run code during crash

扶醉桌前 提交于 2019-12-02 17:49:15

问题


I had a bad crash case that was caused due to some Asyncs doing stuff in improper order in a SQLite and thing blew up. It took me some time to debug all that and access to the internal db would have helped immensely. I know how to access that internal db on a dev device but in case something goes wrong I would like to be able to get an instance of that db no matter the device. For error reporting I am using Crashlytics.

The question is: Is there a way to have Crashlytics run a piece of code (method, etc) during the crash collection/reporting? (For example, get db copy and email it, or something)

Couldn't find something in the documentation.


回答1:


It is possible to get control prior to Crashlytics logging a crash. You essentially have to create your own uncaught exception handler and call Crashlytics' handler from there. Something like this in your Application class:

private UncaughtExceptionHandler originalUncaughtHandler;

@Override
public void onCreate() {
    // initialize Fabric with Crashlytics

    originalUncaughtHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(this);

    // do the rest of your oncreate stuff
}

@Override
public void uncaughtException(Thread thread, Throwable ex) {
    // do your work to add data to Crashlytics log

    originalUncaughtHandler.uncaughtException(thread, ex);
}



回答2:


No you can't. You can however set certain values before initiating Crashlytics. Like adding values to parameters so as to identify user. Like adding email id of user before creating a crashlytics session.




回答3:


As @basu-singh said, you can add context to the crash, see https://docs.fabric.io/android/crashlytics/enhanced-reports.html

Or you can use your own UncaughtExceptionHandler, and then call Crashlytics. Though your code needs to be extra safe !



来源:https://stackoverflow.com/questions/43281829/android-crashlytics-run-code-during-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!