Application

2022-05-27  本文已影响0人  Android一行代码
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    new Handler(getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Looper.loop();//try-catch主线程的所有异常;Looper.loop()内部是一个死循环,出现异常时才会退出,所以这里使用while(true)。
                } catch (Throwable e) {
                    Log.d("TAG", "Looper.loop(): " + e.getMessage());
                }
            }
        }
    });

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            //try-catch子线程的所有异常。
            Log.d("TAG", "UncaughtExceptionHandler: " + e.getMessage());
        }
    });

}
上一篇 下一篇

猜你喜欢

热点阅读