Find out if Android device is portrait or landscape for normal usage?

前端 未结 8 1468
情话喂你
情话喂你 2020-12-07 22:05

Is there anyway to find out if a device is portrait or landscape by default? In that I mean how you normally use the device.

Most phones have a portrait screen for

8条回答
  •  有刺的猬
    2020-12-07 22:58

    I had a similar issue. Solved it by comparing values of rotation and orientation. There is no need to use sensors or any listeners, just call isDefaultLandscape method whenever you wish.

    private boolean isDefaultLandscape(final Context context)
    {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        int orientation = context.getResources().getConfiguration().orientation;
    
        switch (rotation)
        {
            case Surface.ROTATION_180:
            case Surface.ROTATION_0:
            {
                return orientation == Configuration.ORIENTATION_LANDSCAPE;
            }
            default:
            {
                return orientation == Configuration.ORIENTATION_PORTRAIT;
            }
        }
    }
    

提交回复
热议问题