Android 如何从传感器确定设备方向

Android 如何从传感器确定设备方向,android,orientation,screen-orientation,android-sensors,android-orientation,Android,Orientation,Screen Orientation,Android Sensors,Android Orientation,我的活动锁定在风景上。但我仍然需要知道设备的方向。所以我选择使用传感器。我有以下代码 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL); ... @Overri

我的活动锁定在风景上。但我仍然需要知道设备的方向。所以我选择使用传感器。我有以下代码

sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
...
@Override
  public void onSensorChanged(SensorEvent event) {
    float[] values = event.values;

    // Movement
    float azimuth = values[0];
    float pitch = values[1];
    float roll = values[2];

    if ((-110 <= pitch && pitch <= -70) || (70 <= pitch && pitch <= 110)) {
      //PORTRAIT MODE
      portraitPitch = true;
      landscapePitch = false;
      Log.d(TAG, "portrait mode: pitch = " + pitch);
    } else if ((-20 <= pitch && pitch <= 20) || (-200 <= pitch && pitch <= -160) || (160 <= pitch && pitch <= 200)) {
      //LANDSCAPE MODE
      portraitPitch = false;
      landscapePitch = true;
      Log.d(TAG, "landscape mode : pitch = " + pitch);
    }

    if ((-20 <= roll && roll <= 20)) {
      //PORTRAIT MODE
      portraitRoll = true;
      landscapePitch = false;
      Log.d(TAG, "portrait mode: roll = " + roll);
    } else if ((-110 <= roll && roll <= -70) || (70 <= roll && roll <= 110)) {
      //LANDSCAPE MODE
      portraitRoll = false;
      landscapePitch = true;
      Log.d(TAG, "landscape mode : roll = " + roll);
    }

    if (portraitPitch && portraitRoll && !portrait) {
      portrait = true;
      landscape = false;
      rotateIconsToPortraitMode();
      Log.d(TAG, "portrait mode for icons: pitch = " + pitch + ", roll = " + roll);
    }

    if (landscapePitch && landscapeRoll && !landscape) {
      landscape = true;
      portrait = false;
      rotateIconsToLandscapeMode();
      Log.d(TAG, "landscape mode for icons: pitch = " + pitch + ", roll = " + roll);
    }
}
sensorManager=(sensorManager)getSystemService(SENSOR\u SERVICE);
sensorManager.registerListener(这是sensorManager.getDefaultSensor(Sensor.TYPE\u ORIENTATION),sensorManager.Sensor\u DELAY\u NORMAL);
...
@凌驾
传感器更改时的公共无效(传感器事件){
float[]值=event.values;
//运动
浮动方位角=值[0];
浮动节距=值[1];
浮动辊=数值[2];

如果((-110如果横向与纵向方向是您所需要的,那么音高值应该足够。根据您的具体需要,音高值可能并不完美。但根据我的理解,您可以简单地使用音高。因此,您可以完全按照现有的方式使用音高值,只需忽略滚动。

oncreate
中实例化
方向ventlister

OrientationEventListener orientationEventListener = new OrientationEventListener(this)
    {
        @Override
        public void onOrientationChanged(int orientation)
        {
            Log.d(TAG, "orientation = " + orientation);
        }
    }; 

    orientationEventListener.enable();

使用方向值,您可以确定设备是纵向还是横向。

您是否尝试过自己处理配置更改?-添加
configChanges:orientation
以显示,并实现
onConfigurationChanged
,然后您可能会得到不起作用的
newConfig.orientation
方向已锁定,配置更改事件没有意义。您必须使用传感器。然后我建议这正是我需要的:)