How to detect screen rotation through 180 degrees from landscape to landscape orientation?

后端 未结 3 1133
天涯浪人
天涯浪人 2020-12-03 10:43

I\'ve seen a few other questions similar to this but no answers.

Rotating from portrait to landscape (either direction) and back again, we get the helpful call to on

3条回答
  •  攒了一身酷
    2020-12-03 11:17

    OrientationEventlistener won't work when the device isn't rotating/moving.

    I find display listener is a better way to detect the change.

         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);
    

提交回复
热议问题