如何正确使用Thread.UncaughtExceptionHandler在所有情况下启动一个Activity?

时间:2014-09-18 16:43:34

标签: android-activity android

我想覆盖整个应用程序中的默认Thread.UncaughtExceptionHandler,以便在出现问题时启动新的错误显示Activity

我能够编写一些代码并让它完成一半工作,但是我遇到的问题是我无法弄清楚如何修复。如果在应用程序的其他活动之一Thread.UncaughtExceptionHandler之后的任何时间发生未捕获的异常,则扩展Activity的类可以启动新的onCreate()。但是,如果在onCreate()中或在屏幕上绘制UI之前发生了未捕获的异常,则我的错误显示Activity无法启动,我的应用程序就会挂起。

我可以通过保存默认Thread.UncaughtExceptionHandler并将未被捕获的Throwable传递给uncaughtException()来反转此行为。这会导致在其他活动的onCreate()中处理异常,但是onCreate()之后的任何异常只会启动默认值"不幸的是,应用程序已停止。"对话框和我的错误显示Activity无法启动。

......代码!

public class MyApplication extends Application {
    private Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler;
    private ExceptionRouter exceptionRouter = null;

    public MyApplication() {
        defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        exceptionRouter = new ExceptionRouter();
        Thread.setDefaultUncaughtExceptionHandler(exceptionRouter);
    }

    public class ExceptionRouter implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            launchErrorActivity(throwable);

            // Uncommenting the following line makes what I want to do work in other Activities' onCreate() but not after
            // Leaving the line commented out makes what I want to do work after other Activities' onCreate() but not in it
            // defaultUncaughtExceptionHandler.uncaughtException(thread, throwable);
        }

        private void launchErrorActivity(Throwable throwable) {
            Context context = getApplicationContext();
            Intent errorIntent = new Intent(context, ErrorActivity.class);
            errorIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            Bundle extras = new Bundle();
            extras.putSerializable("throwable", throwable);
            errorIntent.putExtras(extras);

            context.startActivity(errorIntent);
        }
    }
}

我已经确认在所有情况下都达到了context.StartActivity(errorIntent);行。

我可以做些什么来解决我的问题?

1 个答案:

答案 0 :(得分:0)

有一次我需要在异常后显示对话框,我会显示如下所示的对话框。也许这段代码可以帮助你。

public void uncaughtException(Thread thread, Throwable throwable) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(fragmentActivity);
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();

                builder.setTitle(title);
                builder.create();
                builder.setCancelable(false);
                builder.setPositiveButton(positiveButtonText,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                //some action
                            }
                        });
                builder.setMessage(message);
                builder.show();

                Looper.loop();
            }
        }.start();
}