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

前端 未结 1 586
天命终不由人
天命终不由人 2021-02-04 18:46

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

1条回答
  •  误落风尘
    2021-02-04 19:46

    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

    
    

    0 讨论(0)
提交回复
热议问题