崩溃后启动应用程序

时间:2016-11-28 09:33:49

标签: android crash android-pendingintent main-activity

我想在崩溃后重启我的应用程序。所以我在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

1 个答案:

答案 0 :(得分: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;
    }
}