2016-11-28 5 views
0

クラッシュ後にアプリケーションを再起動したい。だから私は、Applicationクラスのクラッシュハンドラを作成しました:クラッシュ後にアプリケーションを起動する

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)メソッド呼び出しを解決し、アプリケーションを再起動しました。しかし、私はクラッシュ・ミックスのクラッシュを見ることができませんでした。一方、私がSystem.exit()に電話しないと、クラッシュリズムがクラッシュすることがありますが、アプリケーションは再起動しません。だからこそ私はSystem.exit(2)メソッドを実行し、mDefaultUEH.uncaughtException(thread, throwable)を並列にExecutorService経由で実行したのです。ここに作業コード:

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; 
    } 
} 
関連する問題