Android 8.0 设置横竖屏报错问题

项目代码中设置手机为竖屏报错了,报了 Only fullscreen activities can request orientation. 报错代码如下
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

网上有多种方法,最后用了一个反射顺利解决,代码如下:

if (Build.VERSION.SDK_INT >= 26) {
        convertActivityFromTranslucent(this);
}

public static void convertActivityFromTranslucent(Activity activity) {
        try {
            Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
            method.setAccessible(true);
            method.invoke(activity);
        } catch (Throwable t) {
        }
 }

报错原因可以参考:https://zhuanlan.zhihu.com/p/32190223

你可能感兴趣的:(Android 8.0 设置横竖屏报错问题)