How to have a callback for when a crash occurred, while using Crashlytics SDK?

断了今生、忘了曾经 提交于 2019-12-03 13:58:48

问题


Background

We use Crashlytics SDK to manage app crashes and get needed information about them.

So far, the information that the SDK automatically gathered was enough

The problem

I'd like to add more information for each crash, such as: available&total heap memory, activity stack,...

Thing is, I don't see a way to achieve this.

I know that the way Android framework works with unhandled exceptions is pretty easy (using Thread.setDefaultUncaughtExceptionHandler) and it's probably how the SDK works, but I can't find where to use the listener of the SDK itself.

What I've tried

  1. The SDK has a listener, but it seems it's not of the current session, as shown here. The function name is "crashlyticsDidDetectCrashDuringPreviousExecution" , meaning it's of the previous session. Same callback was available before in deprecated methods.

  2. There are "Custom Logging" and "Custom Keys" features, but those occur when I call them (not right when the crash occurs).

The question

Is there a way to add extra information to Crashlytics right when a crash occurs ?

If so, how?


回答1:


Try creating an UncaughtExceptionHandler and use Custom Key(s) to store the information you want to be associated with your crash report.

  1. Create your custom UncaughtExceptionHandler (ensuring that it will pass exception to default UncaughtExceptionHandler to be handled later via Crashlytics).
  2. In the uncaughtException method add custom logic to set your key e.g. Crashlytics.setString("available_memory", "5784");

  3. Check your Crashlytics dashboard to view your custom key(s) when your app crashes

Create a custom Application subclass to hold your logic:

public class MyApplication extends Application {
   private static Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;

   private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
       @Override
       public void uncaughtException(Thread thread, Throwable ex) {
          // Custom logic goes here
          // Calculate available memory
          Crashlytics.setString("available_memory", "5784");
          // This will make Crashlytics do its job
          mDefaultUncaughtExceptionHandler.uncaughtException(thread, ex);
       }
   };

   @Override
   public void onCreate() {
     super.onCreate();

     // Order is important!
     // First, start Crashlytics
     Crashlytics.start(this);

     // Second, cache a reference to default uncaught exception handler
     mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
     // Third, set custom UncaughtExceptionHandler
     Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
   }
}

Remember to specify the name of your Application subclass in your AndroidManifest.xml’s tag

<application android:name="MyApplication">


来源:https://stackoverflow.com/questions/37256595/how-to-have-a-callback-for-when-a-crash-occurred-while-using-crashlytics-sdk

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