Crashlytics Android SDK - custom UncaughtExceptionHandler

前端 未结 6 1994
星月不相逢
星月不相逢 2020-11-29 05:34

Is it possible to incorporate custom UncaughtExceptionHandler along with crashlytics in one application? If yes - how?

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 06:36

    Yes, it is possible.

    In your Application class:

    @Override
    public void onCreate() {
        super.onCreate();
        Crashlytics.start(this);
        initUncaughtExceptionHandler();
    }
    
    private void initUncaughtExceptionHandler() {
        final ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
        c.schedule(new Runnable() {
            @Override
            public void run() {
                final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
                Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                        // do my amazing stuff here 
                        System.err.println("Error!");
                        //then pass the job to the previous handler
                        defaultHandler.uncaughtException(paramThread, paramThrowable);
                    }
                });
            }
        }, 5, TimeUnit.SECONDS);
    }
    

    The reason I'm scheduling this after 5 seconds is because Crashlytics needs some time to set up his stuff. I'm using this code and it works perfectly. Of course if your app crashes on start, sorry but no custom handler ;)

提交回复
热议问题