Android 如何在不使用加速计的情况下获得屏幕方向?

Android 如何在不使用加速计的情况下获得屏幕方向?,android,screen-orientation,Android,Screen Orientation,如何得到它?在我的XML文件中,我对我的活动使用唯一的纵向方向 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout" android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden" 和OnConfiguration已更改,不适用于定向 我目前使用的Upd: @Override public void o

如何得到它?在我的XML文件中,我对我的活动使用唯一的纵向方向

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden"
和OnConfiguration已更改,不适用于定向

我目前使用的Upd:

@Override
    public void onSensorChanged(SensorEvent event) {
        x = event.values[0];
        y = event.values[1];
    }

    private PictureOrientation getLastOrientation() {
        MyPictureOrientation orient;
        if (x >= -5 && x < 5) {
            if (y > 0) {
                orient = PORTRAIT;
            } else {
                orient = PORTRAIT_REV;
            }
        } else {
            if (x >= -10 && x < -5) {
                orient = LAND_RIGHT;
            } else {
                orient = LAND_LEFT;
            }
        }

        return orient;
    }
@覆盖
传感器更改时的公共无效(传感器事件){
x=事件值[0];
y=事件值[1];
}
private PictureOrientation getLastOrientation(){
MyPictureOrientation;
如果(x>=-5&&x<5){
如果(y>0){
东方=肖像;
}否则{
方向=纵向\反向;
}
}否则{
如果(x>=-10&&x<-5){
方向=土地权;
}否则{
东方=左陆;
}
}
回归导向;
}

您可以使用以下代码获取当前方向:

getResources().getConfiguration().orientation
一些伪代码:

Display getOrient = getWindowManager().getDefaultDisplay();
if(getOrient.getWidth() < getOrient.getHeight())
  // then you have portrait orientation
else
  // you have landscape
Display getOrient=getWindowManager().getDefaultDisplay();
if(getOrient.getWidth()
如果你想让你的应用程序在potrait中运行,只需在清单中添加以下内容

android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait"
在oncreate和onConfiguration上更改了put

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

getResources().getConfiguration().orientation-始终==1不适合我。纵向方向的W和H不能更改页面是否更改为横向?删除android后重试:configChanges=“keyboardHidden | orientation”