修复Android8.0系统BUG导致透明度+转向崩溃

2023-01-19  本文已影响0人  清明捉鬼
    @Override
    public void onCreate(Bundle savedInstanceState) {
        fixOnlyFullscreen();
        super.onCreate(savedInstanceState);
    }
    /**
     * 修复透明+转向的Android8.0系统 BUG
     */
    private void fixOnlyFullscreen(){
        if (Build.VERSION.SDK_INT == 26 && isTranslucentOrFloating()) {
            fixOrientation(this);
        }
    }

    /**
     *  hook反射方向检查
     **/
    private static void fixOrientation(Activity activity) {
        try {
            Class activityClass = Activity.class;
            Field mActivityInfoField = activityClass.getDeclaredField("mActivityInfo");
            mActivityInfoField.setAccessible(true);
            ActivityInfo activityInfo = (ActivityInfo) mActivityInfoField.get(activity);
            //设置屏幕不固定
            activityInfo.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
        } catch (Exception e) {
        }
    }

    /**
     * hook反射检查是否透明色或者悬浮
     **/
    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray typedArray = obtainStyledAttributes(styleableRes);
            Method method = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            method.setAccessible(true);
            isTranslucentOrFloating = (boolean) method.invoke(null, typedArray);
            method.setAccessible(false);
        } catch (Exception e) {
        }
        return isTranslucentOrFloating;
    }

代码引用自 https://blog.csdn.net/badme/article/details/126599589

上一篇下一篇

猜你喜欢

热点阅读