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()方法不調用。所以屏幕凍結,沒有顯示(只有白屏)。它不響應,也不會崩潰。下面是截圖:
這真的不可靠。您可以嘗試添加一些延遲,以便在重新啓動應用程序之前讓舊進程正常關閉。嘗試5或10秒後觸發警報,看看是否有幫助。 –