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