我想获得我的设备的实际方向。我不想像这样得到布局:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
我想获得物理设备方向。有没有办法在Android中获得它?
答案 0 :(得分:2)
使用方向更改侦听器
OrientationEventListener mOrientatinChangeListener = new OrientationEventListener(getApplicationContext(), SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
// Here you will get the actual orientation
}
};
protected void onStart() {
super.onStart();
// Activate the Orientation change listener
mOrientatinChangeListener.enable();
}
注意:如果您希望陀螺仪/ accellerometer的三维方向,那么Using Android gyroscope instead of accelerometer. I find lots of bits and pieces, but no complete code