2016-11-28 7 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; 
    } 
}