Android onConfigurationChanged 收不到回调

我返现,90度横屏 旋转到270度横屏onConfigurationChanged 是收不到回掉的。尽管清单里面声明了什么:
android:configChanges=”orientation|keyboardHidden|navigation|screenSize|layoutDirection|smallestScreenSize|screenLayout|mnc”

没用。

那怎么办? 通过监听手机旋转,自己判断吧:

    private int mIsLandRightOrientation = -1;            //是否已经处于横屏状态,-1为未初始化,1为非左横屏(手机刘海朝左边的情况,即90度),2为右横屏(手机刘海朝右边的情况,及270度)

    private OrientationEventListener mOrientationEventListener; //旋转监听 因为异形屏需要知道横屏从90旋转到了270 而添加
    private Display mDisplay;


           mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_STATUS_ACCURACY_LOW){
                @Override
                public void onOrientationChanged(int orientation) {
                    int ro = orientation % 360;
                    // 设置横屏 260
                    if (((ro > 260) && (ro < 315))) {//当前手机刘海方向朝左边的情况
                        if (mIsLandRightOrientation == 2 || mIsLandRightOrientation == -1) {//如果说之前的方向是右边,往下处理 否则不处理
                            if (getScreenRotationOnPhone() == Surface.ROTATION_90) {//如果当前屏幕的布局方向是相左,说明屏幕旋转了 那么进去处理paddding
                                mIsLandRightOrientation = 1;
                                adjustPadding();
                            }
                        }
                    }else if((ro > 80 && ro < 135)){//手机刘海朝右边的情况
                        if (mIsLandRightOrientation == 1 || mIsLandRightOrientation == -1) {
                            if (getScreenRotationOnPhone() == Surface.ROTATION_270) {
                                mIsLandRightOrientation = 2;
                                adjustPadding();
                            }
                        }
                    }
                }
            };
            mOrientationEventListener.enable();


    private int getScreenRotationOnPhone() {
        if (mDisplay == null) {
            mDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        }
        return mDisplay!=null ? mDisplay.getRotation():-1;
    }

原理就是通过监听手机旋转,然后在根据屏幕方向,精确判断手机横屏90度到270的转换。

原文地址:https://www.cnblogs.com/caoxinyu/p/10568551.html