我一直在尝试在我的应用程序中实现自定义 UncaughtExceptionHandler,并且我发现了库 Ereza/CustomActivityOnCrash。
我已经添加了自述文件( https://github.com/Ereza/CustomActivityOnCrash )中描述的所有代码,但我似乎仍然无法让它工作。当我强制我的应用程序崩溃时,会弹出一个白屏,然后立即消失。
有什么想法可能导致这种情况吗?下面是我的一些代码
AndroidManifest
<activity
android:name="com.test.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.test.MainActivity"
android:label="MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="cat.ereza.customactivityoncrash.RESTART" />
</intent-filter>
</activity>
<activity
android:name="com.test.ErrorActivity"
android:label="Error"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="cat.ereza.customactivityoncrash.ERROR" />
</intent-filter>
</activity>
应用类
public class TestApplication extends Application {
private static TestApplication mInstance;
private TimeZone usersDefaultTimeZone;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
//Customization for CustomActivityOnCrash
//Show (TRUE) or Hide (FALSE) the stack trace on the error activity
CustomActivityOnCrash.setShowErrorDetails(false);
//Enable Restart (TRUE) 'Restart App' button
//Disable Restart (FALSE) 'Close App' button
CustomActivityOnCrash.setEnableAppRestart(true);
//This sets a custom error activity class instead of the default one.
CustomActivityOnCrash.setErrorActivityClass(ErrorActivity.class);
//This sets a EventListener to be notified of events regarding the error activity,
//CustomActivityOnCrash.setEventListener(new CustomEventListener());
//Specify the Activity to to restart the application
CustomActivityOnCrash.setRestartActivityClass(MainActivity.class);
//Install CustomActivityOnCrash
CustomActivityOnCrash.install(this);
//Now initialize your error handlers as normal
Fabric.with(this, new Crashlytics());
}
public static synchronized TestApplication getInstance() {
return mInstance;
}
static class CustomEventListener implements CustomActivityOnCrash.EventListener{
@Override
public void onLaunchErrorActivity() {
Log.i("BookingApp", "onLaunchErrorActivity()");
Crashlytics.logException(new Exception("Unknown Exception Caught & Error screen displayed to user"));
}
@Override
public void onRestartAppFromErrorActivity() {
Log.i("BookingApp", "onRestartAppFromErrorActivity()");
Answers.getInstance().logCustom(new CustomEvent("Restart After Crash"));
}
@Override
public void onCloseAppFromErrorActivity() {
Log.i("BookingApp", "onCloseAppFromErrorActivity()");
Answers.getInstance().logCustom(new CustomEvent("Close After Crash"));
}
}
}