1

我有以下代碼來處理任何未捕獲的異常,並從Splash屏幕重新啓動應用程序,因爲我在Splash Screen中執行了大量的初始化操作。這是我的啓動屏幕。嘗試在未捕獲的異常後啓動特定活動時掛起的應用程序

現在我有下面的代碼來實現此功能:在startActivity(reStartIntent);

@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); 


    } 

不過是掛起,出現黑屏。

有人能幫我理解這裏發生了什麼嗎?

回答

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); 
} 

編輯:要小心 - 一些崩潰可能會導致循環 - 你可能要檢查這個!

+0

我的崩潰是一個空指針異常,我也有與您的代碼相同的問題。 – Sunny 2014-10-11 14:39:10

+0

顯示你的堆棧跟蹤 – ligi 2014-10-11 14:41:16

+0

好吧,你是對的有一個堆棧溢出 – Sunny 2014-10-11 14:48:49

相關問題