Catch all possible android exception globally and reload application

后端 未结 3 809
时光说笑
时光说笑 2020-12-13 15:23

I know the best way to prevent system crashes is catching all possible exception in different methods. So I use try catch blocks every where in my code. However as you know

3条回答
  •  无人及你
    2020-12-13 15:56

    There you go:

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
    
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(getActivity(),"Your message", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }.start();
            try
            {
                Thread.sleep(4000); // Let the Toast display before app will get shutdown
            }
            catch (InterruptedException e) {    }
            System.exit(2);
        }
    });
    

提交回复
热议问题