在未捕获的异常后尝试启动特定活动时应用程序挂起

时间:2014-10-11 14:24:32

标签: android android-intent exception-handling uncaughtexceptionhandler

我有以下代码来处理任何未捕获的异常并从Splash屏幕重新启动应用程序,因为我在Splash Screen中执行了大量的初始化。这是我的启动画面。

现在我有以下代码:

@Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // restart Application
        Log.e("OSRAM Lightify", "LightifyApplication: UNCAUGHT EXCEPTION FOUND: \n" + ex.getStackTrace());

        Intent reStartIntent = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        reStartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(reStartIntent);


    }

但挂起在startActivity(reStartIntent);,屏幕显示为空白。

有人可以帮我理解这里发生了什么吗?

1 个答案:

答案 0 :(得分:0)

可能有很多原因造成这种情况 - 在此之后你想要像以下那样重新开始:

@Override
public void uncaughtException(final Thread thread, final Throwable ex) {

    LOG.error("", ex);

    installRestartIntent();

    System.exit(2);
}

private void installRestartIntent() {

    Intent rescueIntent = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage(getBaseContext().getPackageName());
    rescueIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    final PendingIntent pendingIntent = PendingIntent.getActivity(application,
                                                                  0,
                                                                  rescueIntent,
                                                                  rescueIntent.getFlags());
    final AlarmManager alarmManager = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, DateTime.now().plusSeconds(1).getMillis(), pendingIntent);
}
编辑:小心 - 一些崩溃可能会导致循环 - 你可能想检查一下!