android - Firebase Crashlytics 与 UncaughtExceptionHandler

标签 android firebase crashlytics

我已经集成了 Firebase Crashlytics 2.9.1 版来挖掘崩溃,以涵盖我的应用程序的性能和稳定性。

如果应用程序有自己的 UncaughtExceptionHandler,则不会在 firebase crashlytics 控制台上记录崩溃。

我的应用中有 BaseActivity。
在 onCreate() 方法中,我根据项目要求注册了自定义 UncaughtExceptionHandler。

每当应用程序因任何原因崩溃时,应将用户重定向到启动屏幕 (MainActivity.java)。

public class BaseActivity extends FragmentActivity{ 

@Override 
protected void onCreate(Bundle arg0) { 
   // Enable global crash handler. 
   Thread.setDefaultUncaughtExceptionHandler(handleAppCrash); 
} 

/*** 
* @Purpose Called when any crash occurs in the application. 
***/ 
private Thread.UncaughtExceptionHandler handleAppCrash = new Thread.UncaughtExceptionHandler() { 
@Override 
public void uncaughtException(Thread thread, Throwable ex) { 

   Intent intent = new Intent(context, MainActivity.class); //redirect to Splash screen
   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
   context.startActivity(intent); 
   System.exit(0); 
  } 
}; 

} 

最佳答案

好的,我已经调查过这个问题。

您不应在 BaseActivity 中创建自定义异常处理程序。
最好在您的应用程序类中执行此操作。
在 BaseActivity 的情况下,每次启动新 Activity 时都会有一个新的处理程序,这会扩展您的 BaseActivity。

因此,在您的应用程序类 onCreate() 方法中,您可以获得默认的应用程序处理程序

val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()

在我的情况下,它是一个实例
com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler

您将使用此“defaultExceptionHandler”向 Firebase 发送正确的错误。
然后你可以创建自己的异常处理程序,但需要在这里保存这个“defaultExceptionHandler”。
class DefaultExceptionHandler (private val  defaultExceptionHandler:Thread.UncaughtExceptionHandler?) : Thread.UncaughtExceptionHandler {
override fun uncaughtException(thread: Thread, ex: Throwable) {
        try {       
            // here you restore your activity and do other things
            // and of course you deal with defaultExceptionHandler
            // without it firebase will not work       
            defaultExceptionHandler?.uncaughtException(thread, ex)
            // only after firebase dealed with an exception, you can exit
            System.exit(0)
        } catch (e: IOException) {
            // just catch
        }

        }
    }

最后,firebase 会根据需要向您显示崩溃。
应用程序 onCreate() 示例
override fun onCreate() {
        super.onCreate()
        Fabric.with(this, Crashlytics())
        val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
        val customExceptionHandler = DefaultExceptionHandler(defaultExceptionHandler)
        Thread.setDefaultUncaughtExceptionHandler(customExceptionHandler)
    }

关于android - Firebase Crashlytics 与 UncaughtExceptionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585187/

相关文章:

android - 无法解决 : com. crashlytics.sdk.android :answers-shim:0. 0.3

ios - 使用上传符号将 dSYM 上传到 Firebase

android - Kotlin 版本 1.1.4-eap-77 不工作

android - listfragment 中的第二个 listview 不滚动

ios - 如何在 Firebase DB 中更新子项的特定值

javascript - 重定向后如何将用户带到完全相同的位置?

没有 Google Play 服务的 Firebase Crashlytics

android - Google Play 服务版本

我的应用程序的 Android APK 下载不起作用,Xamarin.Android

firebase函数:shell can't find custom functions configuration variables