2016-11-28 246 views
0

我想在崩潰後重新啓動我的應用程序。因此,我創建的應用類中的崩潰處理:崩潰後啓動應用程序

public final class MyApp extends Application { 
    private static Thread.UncaughtExceptionHandler mDefaultUEH; 


    private Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { 
     @Override 
     public void uncaughtException(Thread thread, Throwable ex) { 


      Intent splashIntent = new Intent(getInstance().getActivity(), A1SplashScreen.class); 

      //splashIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
      //splashIntent.setAction(Intent.ACTION_MAIN); 
      splashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      splashIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      int requestID = (int) System.currentTimeMillis(); 
      PendingIntent pending = PendingIntent.getActivity(getInstance().getActivity(), requestID, splashIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT); 
      AlarmManager alarmManager = (AlarmManager) getInstance().getActivity().getSystemService(ALARM_SERVICE); 
      alarmManager.set(AlarmManager.RTC, System.currentTimeMillis(), pending); 

      mDefaultUEH.uncaughtException(thread, ex); 
     } 
    }; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     Fabric.with(this, new Crashlytics()); 
     mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler(); 
     Thread.setDefaultUncaughtExceptionHandler(mCaughtExceptionHandler); 
    } 
} 

A1SplashScreen是應用程序的主要活動,所以我要崩潰後啓動該活動。 API 21及以上版本沒有問題。但問題出在API級別低於21.應用程序崩潰後,A1SplashScreen啓動,但它的onCreate()方法不調用。所以屏幕凍結,沒有顯示(只有白屏)。它不響應,也不會崩潰。下面是截圖: enter image description here

+0

這真的不可靠。您可以嘗試添加一些延遲,以便在重新啓動應用程序之前讓舊進程正常關閉。嘗試5或10秒後觸發警報,看看是否有幫助。 –

回答

0

議決

當我打電話System.exit(2)方法,應用程序重新啓動。但我看不到crashlytics上的崩潰。另一方面,如果我不打電話System.exit(),我可以看到崩潰崩潰,但應用程序不重新啓動。這就是爲什麼我通過ExecutorService運行System.exit(2)方法和mDefaultUEH.uncaughtException(thread, throwable)平行。這裏是工作代碼:

private Thread.UncaughtExceptionHandler mCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { 
    @Override 
    public void uncaughtException(Thread thread, Throwable ex) { 

     Intent intent = new Intent(getApplicationContext(), A1SplashScreen.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
     getInstance().getActivity().startActivity(intent); 

     activity.finish(); 

     ExecutorService executorService = Executors.newCachedThreadPool(); 
     CallCrashlytics callCrashlytics = new CallCrashlytics(thread, ex); 
     CallSystemExit callSystemExit = new CallSystemExit(); 

     try { 
      executorService.invokeAll(Arrays.asList(callCrashlytics, callSystemExit)); 
     }catch (InterruptedException e){ 
      e.printStackTrace(); 
     } 
    } 
}; 

class CallCrashlytics implements Callable<Void>{ 

    Thread thread; 
    Throwable throwable; 

    CallCrashlytics(Thread thread, Throwable throwable){ 
     this.thread = thread; 
     this.throwable = throwable; 
    } 

    @Override 
    public Void call() throws Exception { 
     mDefaultUEH.uncaughtException(thread, throwable); 
     return null; 
    } 
} 

class CallSystemExit implements Callable<Void>{ 

    @Override 
    public Void call() throws Exception { 
     System.exit(2); 
     return null; 
    } 
}