android 横竖屏不能旋转

最近遇到一个很奇怪的bug,手机所有界面横竖屏不能切换,但是,内置Camera 的拍照按钮可以随着横竖屏可以转动. 
       第一次反应是重力传感器,方向传感器以及加速度传感器是否有效.本地有测试传感器的apk,发现重力传感器失效,方向传感器是正常和加速度传感器是正常的。
      寻找触发横竖屏的方法。
      触发屏幕旋转对应代码:
frameworks/base/services/core/java/com/android/server/policy/WindowOrientationListener.java

    /**
     * Creates a new WindowOrientationListener.
     *
     * @param context for the WindowOrientationListener.
     * @param handler Provides the Looper for receiving sensor updates.
     * @param rate at which sensor events are processed (see also
     * {@link android.hardware.SensorManager SensorManager}). Use the default
     * value of {@link android.hardware.SensorManager#SENSOR_DELAY_NORMAL
     * SENSOR_DELAY_NORMAL} for simple screen orientation change detection.
     *
     * This constructor is private since no one uses it.
     */
    private WindowOrientationListener(Context context, Handler handler, int rate) {
        mHandler = handler;
        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mRate = rate;
        //搜索所有的TYPE_DEVICE_ORIENTATION 的senor.
        List l = mSensorManager.getSensorList(Sensor.TYPE_DEVICE_ORIENTATION);
        Sensor wakeUpDeviceOrientationSensor = null;
        Sensor nonWakeUpDeviceOrientationSensor = null;
        /**
         *  Prefer the wakeup form of the sensor if implemented.
         *  It's OK to look for just two types of this sensor and use
         *  the last found. Typical devices will only have one sensor of
         *  this type.
         */
      //遍历所有TYPE_DEVICE_ORIENTATION 的senor
        for (Sensor s : l) {
            if (s.isWakeUpSensor()) {
                wakeUpDeviceOrientationSensor = s;
            } else {
                nonWakeUpDeviceOrientationSensor = s;
            }
        }
   
        if (wakeUpDeviceOrientationSensor != null) {
            mSensor = wakeUpDeviceOrientationSensor;
        } else {
            mSensor = nonWakeUpDeviceOrientationSensor;
        }

        if (mSensor != null) {
            mOrientationJudge = new OrientationSensorJudge();
        }

        if (mOrientationJudge == null) {

         //  USE_GRAVITY_SENSOR 定义死为false.
         //   private static final boolean USE_GRAVITY_SENSOR = false;
         //也就是说只要mOrientationJudge为null,则获取mSensor是TYPE_ACCELEROMETER 类型

            mSensor = mSensorManager.getDefaultSensor(USE_GRAVITY_SENSOR
                    ? Sensor.TYPE_GRAVITY : Sensor.TYPE_ACCELEROMETER);
            if (mSensor != null) {
                // Create listener only if sensors do exist
                mOrientationJudge = new AccelSensorJudge(context);
            }
        }
        if (mWindowManagerDebugger.WMS_DEBUG_USER) {
            Slog.d(TAG, "ctor: " + this);
        }
    }
   从代码中发现涉及三个传感器:方向传感器(ORIENTATION),重力传感器(GRAVITY)和加速度传感器(ACCELEROMETER),但是,重力传感器在此代码根本就没有使用.实际使用的是方向传感器和加速度传感器。

你可能感兴趣的:(横竖屏切换)