voltear teclado samsung rotar rotacion puedo porque pantalla girar gira como celular android

android - teclado - ¿Cómo detectar la rotación de la pantalla a través de 180 grados de orientación horizontal a horizontal?



rotar pantalla (3)

OrientationEventlistener no funcionará cuando el dispositivo no esté girando / moviéndose.

Encuentro que el oyente de pantalla es una mejor manera de detectar el cambio.

DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() { @Override public void onDisplayAdded(int displayId) { android.util.Log.i(TAG, "Display #" + displayId + " added."); } @Override public void onDisplayChanged(int displayId) { android.util.Log.i(TAG, "Display #" + displayId + " changed."); } @Override public void onDisplayRemoved(int displayId) { android.util.Log.i(TAG, "Display #" + displayId + " removed."); } }; DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); displayManager.registerDisplayListener(mDisplayListener, UIThreadHandler);

He visto algunas otras preguntas similares a esta pero no hay respuestas.

Al girar de vertical a horizontal (en cualquier dirección) y viceversa, recibimos la útil llamada a onConfigurationChanged ().

Sin embargo, al girar de un paisaje a otro (hasta 180 grados) no se llama onConfigurationChanged ().

He visto una mención al uso de OrientationEventListener, pero me parece que esto no funciona bien porque puede rotar rápidamente sin activar un cambio de orientación de la pantalla.

He intentado agregar un oyente de cambio de diseño, pero sin éxito.

Entonces, la pregunta es, ¿cómo detectar de manera confiable tal cambio en la orientación del paisaje?


Puede ser que debas agregar algún código lógico en tu OrientationEventListener como este:

mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); OrientationEventListener orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { Display display = mWindowManager.getDefaultDisplay(); int rotation = display.getRotation(); if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) { Log.i(TAG, "changed >>> " + rotation); // do something mLastRotation = rotation; } } }; if (orientationEventListener.canDetectOrientation()) { orientationEventListener.enable(); }


Uso este código para que funcione en mi caso.

OrientationEventListener mOrientationEventListener = new OrientationEventListener(mActivity) { @Override public void onOrientationChanged(int orientation) { if (orientation == ORIENTATION_UNKNOWN) return; int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: android.util.Log.i(TAG, "changed ROTATION_0 - " + orientation); break; case Surface.ROTATION_90: android.util.Log.i(TAG, "changed ROTATION_90 - " + orientation); break; case Surface.ROTATION_180: android.util.Log.i(TAG, "changed ROTATION_180 - " + orientation); break; case Surface.ROTATION_270: android.util.Log.i(TAG, "changed ROTATION_270 - " + orientation); break; } if ((rotation != mLastRotation) && (rotation & 0x1) == (mLastRotation & 0x1)) { android.util.Log.i(TAG, "unhandled orientation changed >>> " + rotation); } mLastRotation = rotation; } }; if (mOrientationEventListener.canDetectOrientation()){ mOrientationEventListener.enable(); }