在一个应用中,是否可以将自定义的UncaughtExceptionHandler与crashlytics结合使用?如果可以-如何实现?
在一个应用中,是否可以将自定义的UncaughtExceptionHandler与crashlytics结合使用?如果可以-如何实现?
更新
请参考@kmityak的答案,因为Crashlytics/Fabric初始化现在是异步的,所以下面的解决方案已不再有效。
原始答案
您可以设置自定义的UncaughtExceptionHandler,只要它将异常传递给默认的UncaughtExceptionHandler以便稍后通过Crashlytics处理。
以下代码是在Application子类中实现的:
private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Custom logic goes here
// This will make Crashlytics do its job
mDefaultUEH.uncaughtException(thread, ex);
}
};
@Override
public void onCreate() {
super.onCreate();
// Order is important!
// First, start Crashlytics
Crashlytics.start(this);
// Second, set custom UncaughtExceptionHandler
mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}
第二个选项是在设置自定义UncaughtExceptionHandler之后注册Crashlytics - 然后所有未捕获的异常都将被Crashlytics报告为致命错误,并在此之后传递给您的自定义处理程序。
Crashlytics.start(this)
方法。我正在使用crashlytics:2.5.1
版本。我希望在退出应用程序的同时将故障报告发送到Fabric。请问该怎么做? - SweetWisher ツFabric.with(this, Crashlytics())
代替Crashlytics.start(this)
。 - Tej由于最近版本的Crashlytics进行异步初始化,因此最好使用Fabric的初始化回调:
private static Thread.UncaughtExceptionHandler mDefaultUEH;
private static Thread.UncaughtExceptionHandler mCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Custom logic goes here
// This will make Crashlytics do its job
mDefaultUEH.uncaughtException(thread, ex);
}
};
CrashlyticsCore core = new CrashlyticsCore.Builder()
.disabled(BuildConfig.DEBUG)
.build();
Fabric.with(new Fabric.Builder(this).kits(new Crashlytics.Builder()
.core(core)
.build())
.initializationCallback(new InitializationCallback<Fabric>() {
@Override
public void success(Fabric fabric) {
mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler);
}
@Override
public void failure(Exception e) {
}
})
.build());
如果那些解决方案都对我无效,我会按照以下方式进行操作:
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
//Send Report to Crashlytics. Crashlytics will send it as soon as it starts to work
Crashlytics.logException(e);
//Your custom codes to Restart the app or handle this crash
HandleCrashes(thread, e);
}
});
以下是我的自定义方法,用于重新启动应用程序:
private void HandleCrashes(Thread t, Throwable e) {
Intent i = new Intent(mContext, frmLogin.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("JustLogin", true);
startActivity(i);
System.exit(1);
}
@Override
public void onCreate() {
super.onCreate();
Crashlytics.start(this);
initUncaughtExceptionHandler();
}
private void initUncaughtExceptionHandler() {
final ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
c.schedule(new Runnable() {
@Override
public void run() {
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
// do my amazing stuff here
System.err.println("Error!");
//then pass the job to the previous handler
defaultHandler.uncaughtException(paramThread, paramThrowable);
}
});
}
}, 5, TimeUnit.SECONDS);
}
我之所以将它设置为5秒后执行,是因为Crashlytics需要一些时间来进行设置。我正在使用这段代码并且它运行得非常完美。当然,如果你的应用在启动时崩溃了,那很抱歉,无法使用自定义处理程序;)
关闭自动收集
将以下内容添加到您的AndroidManifest.xml
文件中
<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />
UncaughtExceptionHandler
Thread.setDefaultUncaughtExceptionHandler(customerUncaughtExceptionHandler)
Fabric.with(this, new Crashlytics());
我找到了 Fabric 2.10.1 的解决方案:
Thread.setDefaultUncaughtExceptionHandler(yourExceptionHandler)
Fabric.with(this, Crashlytics())
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
// Delegate to the default uncaught exception handler provided by the system
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(thread, throwable);
}
});
// Initialize Firebase Crashlytics
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(true);
// Set Firebase Crashlytics as the default uncaught exception handler
Thread.setDefaultUncaughtExceptionHandler(FirebaseCrashlytics.getInstance().getUncaughtExceptionHandler());
.getUncaughtExceptionHandler()
方法适用于哪个版本? - Chisko