Use OrientationEventListener:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { mDeviceOrientation = orientation; } }; if(mOrientationEventListener.canDetectOrientation()) { mOrientationEventListener.enable(); }
mDeviceOrientation should be an integer indicating the angle of rotation of your device, if you do some smart rounding, you should see which of the four orientations it is:
// Divide by 90 into an int to round, then multiply out to one of 5 positions, either 0,90,180,270,360. int orientation = 90*Math.round(mDeviceOrientation / 90); // Convert 360 to 0 if(orientation == 360) { orientation = 0; }
Enjoy it!
jsonfry
source share