【已解决】java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation

一、开发现场:

       开发中使用activity作为二级界面弹出,且设置了透明主题

二,崩溃日志:

Caused by: java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
        at android.app.Activity.onCreate(Activity.java:986)
        at com.xx.xxx.xxxx.activity.BaseActivity.onCreate(BaseActivity.java:43)
        at com.xx.xxx.xxxx.activity.XxxActivity.onCreate(XxxActivity.java:71)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

三、分析原因:

Only fullscreen opaque activities can request orientation  即:只有全屏不透明的activity可以设置orientation

而这个原因是只有8.0上才出现,8.1是已经修复了这个问题的。

本人开发中,是设置了activity的方向是横向,且设置了activity的主题是透明的。

android:screenOrientation="landscape"
    android:windowSoftInputMode="adjustPan|stateHidden"
    android:theme="@style/Theme.Custom.Fullscreen.Transparent" />

所以不能使用网上的解决方案,干掉方向或者透明主题其中一个,只能选择第三种方法,看看如何在兼容两者的情况下去hook绕过该方向检查,不需要修改xml的配置。

看一下8.0系统的Activity源代码:

@MainThread
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);

    if (getApplicationInfo().targetSdkVersion > O && mActivityInfo.isFixedOrientation()) {
         final TypedArray ta = obtainStyledAttributes(com.android.internal.R.styleable.Window);
         final boolean isTranslucentOrFloating = ActivityInfo.isTranslucentOrFloating(ta);
         ta.recycle();

         if (isTranslucentOrFloating) {
             throw new IllegalStateException(
                        "Only fullscreen opaque activities can request orientation");
         }
    }
    //省略部分源代码....
} 


//查看ActivityInfo的isTranlucentOrFloating()方法
/**
 * Determines whether the {@link Activity} is considered translucent or floating.
 * @hide
*/
public static boolean isTranslucentOrFloating(TypedArray attributes) {
    final boolean isTranslucent =
                attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsTranslucent,
                        false);
    final boolean isSwipeToDismiss = !attributes.hasValue(
                com.android.internal.R.styleable.Window_windowIsTranslucent)
                && attributes.getBoolean(
                        com.android.internal.R.styleable.Window_windowSwipeToDismiss, false);
    final boolean isFloating =
                attributes.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating,
                        false);

    return isFloating || isTranslucent || isSwipeToDismiss;
}

在onCreate()方法中会进行屏幕配置检查。如果固定屏幕方向,且设置了透明色或者悬浮,则会抛出异常。

四、解决方案:

原因基本确定,那么可以考虑是否通过hook反射的方式进行配置的修改,从而绕过检查

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    //切记:在父类oncreate()方法调用前调用该方法修改配置
    if (Build.VERSION.SDK_INT == 26 && isTranslucentOrFloating()) {
        fixOrientation(this);
    }
    super.onCreate(savedInstanceState);
}

/**
 *  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;
}

到此,问题基本解决,总结一下:

1,解题思路:hook判断是否透明色或者悬浮,hook修改activity的屏幕方向为不固定,绕过检查。

2,解题模型:查看日志,锁定位置;查看源代码,分析原因;没有思路可对比&参考解决思路,有思路则直接hook实现;归档总结,避免重复犯错;

你可能感兴趣的:(Android,Java,java,android)