關(guān)于8.0以下出現(xiàn)Only fullscreen opaque activities can request orientation問題的解決方案

原因:

出現(xiàn)Only fullscreen opaque activities can request orientation這個(gè)問題是因?yàn)?.0之前全屏不透明活動(dòng)不可以請(qǐng)求方向 要不然只能設(shè)置不透明,你看看這扯淡不,我要是不設(shè)置透明我還會(huì)碰見這種情況? 那么有沒有辦法解決呢? 那肯定是有的

解決方案:

優(yōu)雅的方式:Hook反射繞過檢查

聽起來是不是很cool

思路:

判斷是否透明色或者懸浮
修改activity的屏幕方向,不固定,繞過檢查。

public class ActivityHook {


    /**
     * java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
     * <p>
     * 修復(fù)android 8.0存在的問題
     * <p>
     * 在Activity中onCreate()中super之前調(diào)用
     *
     * @param activity
     */
    public static void hookOrientation(Activity activity) {
        //目標(biāo)版本8.0及其以上
        if (activity.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
            if (isTranslucentOrFloating(activity)) {
                fixOrientation(activity);
            }
        }
    }

    /**
     * 設(shè)置屏幕不固定,繞過檢查
     *
     * @param activity
     */
    private static void fixOrientation(Activity activity) {
        try {
            Class<Activity> activityClass = Activity.class;
            Field mActivityInfoField = activityClass.getDeclaredField("mActivityInfo");
            mActivityInfoField.setAccessible(true);
            ActivityInfo activityInfo = (ActivityInfo) mActivityInfoField.get(activity);
            //設(shè)置屏幕不固定
            activityInfo.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 檢查屏幕 橫豎屏或者鎖定就是固定
     *
     * @param activity
     * @return
     */
    private static boolean isTranslucentOrFloating(Activity activity) {
        boolean isTranslucentOrFloating = false;
        try {
            Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");
            Field WindowField = styleableClass.getDeclaredField("Window");
            WindowField.setAccessible(true);
            int[] styleableRes = (int[]) WindowField.get(null);
            //先獲取到TypedArray
            final TypedArray typedArray = activity.obtainStyledAttributes(styleableRes);
            Class<?> ActivityInfoClass = ActivityInfo.class;
            //調(diào)用檢查是否屏幕旋轉(zhuǎn)
            Method isTranslucentOrFloatingMethod = ActivityInfoClass.getDeclaredMethod("isTranslucentOrFloating", TypedArray.class);
            isTranslucentOrFloatingMethod.setAccessible(true);
            isTranslucentOrFloating = (boolean) isTranslucentOrFloatingMethod.invoke(null, typedArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }


}

在需要的Activity中使用

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
         
         ActivityHook.hookOrientation(this);//hook,繞過檢查
         
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxxx);

    }

試一下,是不是問題就解決了呢 尿啊~~
大家快去解決問題吧!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容