I have an activity fixed on the manifest to Landscape mode, and I need to detect if the screen rotated to Portrait.
for now, I'm using OrientationEventListener
and play with and put conditions on Orientation values, but I feel that it's not consistent and that there is too much cases to handle, and I'll need to keep track of all orientations values to really keep track of all the passed values, I kinda feel that I'm reinventing the wheel..
private void listenToDeviceRotation () {
OrientationEventListener mOrientationEventListener = new OrientationEventListener (CodesActivity.this) {
@Override
public void onOrientationChanged (int orientation) {
if (orientation == ORIENTATION_UNKNOWN) {
return;
}
Log.e ("-- " + CodesActivity.class.getSimpleName () + " - ", "ORIENTATION : " + orientation);
if (firstOrientation == -1) {
firstOrientation = orientation;
return;
}
if (((orientation >= 0 && orientation < 120) || (orientation > 240 && orientation < 360))) {
finish();
return;
}
}
};
if (mOrientationEventListener.canDetectOrientation()){
mOrientationEventListener.enable();
}
}
Is there any better way to get screen rotation/orientation even though the Activity is fixed to Landscape ?
ps : I target API 17.
Thanks.