使用Crashlytics SDK时,如何在发生崩溃时进行回调? [英] How to have a callback for when a crash occurred, while using Crashlytics SDK?

查看:203
本文介绍了使用Crashlytics SDK时,如何在发生崩溃时进行回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我们使用Crashlytics SDK管理应用崩溃并获取所需的崩溃信息。



到目前为止,SDK自动收集的信息已足够



问题



我想添加更多信息每次崩溃的信息,例如:可用的&总堆内存,活动堆栈,...



问题是,我看不到实现这一目标的方法。 / p>

我知道Android框架处理未处理的异常的方式非常简单(使用 Thread.setDefaultUncaughtExceptionHandler ),这可能是SDK的工作方式,但我做不到找到在哪里使用SDK本身的侦听器。



我尝试过的操作




  1. SDK有一个侦听器,但似乎不是当前版本ssion,如 此处 所示。函数名称为 crashlyticsDidDetectCrashDuringPreviousExecution,表示该名称为上一个会话的名称。在不赞成使用的方法中,以前可以使用相同的回调。


  2. 自定义日志记录 自定义键 功能,但这些功能是在我调用它们时发生的(崩溃时不正确)。




问题



是否可以添加其他信息发生崩溃时对Crashlytics进行评估?



如果是,怎么办?

解决方案

尝试创建UncaughtExceptionHandler并使用自定义密钥来存储您的信息希望与崩溃报告相关联。


  1. 创建自定义UncaughtExceptionHandler(确保将异常传递给默认的UncaughtExceptionHandler,以便稍后通过Crashlytics处理。)。

  2. uncaughtException 方法添加自定义逻辑来设置您的密钥,例如 Crashlytics.setString( available_memory, 5784);


  3. 检查您的Crashlytics信息中心以查看您的应用程序崩溃时的自定义密钥


创建一个自定义应用程序子类来保存您的逻辑:

 公共类MyApplication扩展了应用程序{
私有静态Thread.UncaughtExceptionHandler mDefaultUncaughtExceptionHandler;

私有静态Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler(){
@Override
public void uncaughtException(Thread thread,Throwable ex){
//自定义逻辑在这里
// //计算可用内存
Crashlytics.setString( available_memory, 5784);
//这将使Crashlytics发挥作用
mDefaultUncaughtExceptionHandler.uncaughtException(thread,ex);
}
};

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

//订单很重要!
//首先,启动Crashlytics
Crashlytics.start(this);

//其次,缓存对默认未捕获的异常处理程序的引用
mDefaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
//第三,设置自定义UncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}
}

请记住在您的应用程序中指定Application子类的名称AndroidManifest.xml的标签

 < application android:name = MyApplication> 


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?

解决方案

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">

这篇关于使用Crashlytics SDK时,如何在发生崩溃时进行回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆