美文网首页
Only fullscreen activities can r

Only fullscreen activities can r

作者: 沈杰3 | 来源:发表于2019-03-18 21:05 被阅读0次

    Only fullscreen activities can request orientation终极解决方法

    1. 利用反射,修改mActivityInfo中的变量screenOrientation,设置成SCREEN_ORIENTATION_UNSPECIFIED
    2. override setRequestedOrientation方法,直接return
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
                boolean result = fixOrientation();
                XLog.i(XLog.BASE, "onCreate fixOrientation when Oreo, result = " + result);
            }
            super.onCreate(savedInstanceState);
        }
     
    
        @Override
        public void setRequestedOrientation(int requestedOrientation) {
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
                XLog.i(XLog.BASE, "avoid calling setRequestedOrientation when Oreo.");
                return;
            }
            super.setRequestedOrientation(requestedOrientation);
        }
    
    
        private boolean isTranslucentOrFloating(){
            boolean isTranslucentOrFloating = false;
            try {
                int [] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
                final TypedArray ta = obtainStyledAttributes(styleableRes);
                Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
                m.setAccessible(true);
                isTranslucentOrFloating = (boolean)m.invoke(null, ta);
                m.setAccessible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return isTranslucentOrFloating;
        }
    
        private boolean fixOrientation(){
            try {
                Field field = Activity.class.getDeclaredField("mActivityInfo");
                field.setAccessible(true);
                ActivityInfo o = (ActivityInfo)field.get(this);
                o.screenOrientation = -1;
                field.setAccessible(false);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
    
    

    相关文章

      网友评论

          本文标题:Only fullscreen activities can r

          本文链接:https://www.haomeiwen.com/subject/wxhimqtx.html