0

我一直在尝试在我的应用程序中实现自定义 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"));
        }
    }
}
4

3 回答 3

2

嗨,将以下代码放入 Application 类并根据需要进行修改以处理未处理的异常:

  @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable e) {
                handleUncaughtException(thread, e);
            }
        });
    }

    public void handleUncaughtException(Thread thread, Throwable e) {

            e.printStackTrace(); // not all Android versions will print the stack trace automatically


//Work what you want to do....

//start new activity with clearing task
 Intent intent = new Intent(this, ActivityClass.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

//end
            System.exit(2); // kill off the crashed app
        }

希望这个帮助,我在我的应用程序中使用......有效。

于 2016-05-10T10:35:46.097 回答
0

经过更多调查后,我发现这个库/代码不喜欢在调试模式下运行。如果我在我的设备上安装了应用程序并从那里启动它(而不是使用 Android Studio 中的调试),它似乎工作正常。

于 2016-05-10T11:09:20.973 回答
0

这很可能是由于ErrorActivity没有被指定为另一个进程的一部分。

从库文档中,关于自定义错误活动的使用:

如果您使用它,则必须在您的 中声明该活动AndroidManifest.xml,并process设置为:error_activity

于 2017-05-27T01:46:21.153 回答