クラッシュ後にアプリケーションを再起動したい。だから私は、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()メソッドは呼び出されません。したがって、画面はフリーズし、何も表示されません(白い画面のみ)。それは応答しないし、クラッシュしません。ここではスクリーンショットです:
これは本当に信頼性がありません。あなたは、あなたのアプリを再起動する前に、古いプロセスが正しくシャットダウンできるようにいくつかの遅延を追加することができます。 5秒または10秒後にアラームトリガをして、それが役立つかどうかを確認してください。 –