Android使用Hook完美解决Only fullscreen opaque activities can request orientation的crash问题

今天讲的是target升级为27后高频出现的crash,Only fullscreen opaque activities can request orientation的crash,当targetsdk>=27的时候,且手机Android系统是26,27的时候会crash(华为和小米8.1自己做了优化所以不会出现这个问题,其他品牌手机没有测试)。因为开发的是SDK,所以在适配上很是无奈,SDK提供给使用方,不能限制使用方的targetSdk。如果是APP开发,targetsdk随意设置的话,以下这个仅供参考建议,下面我们就开始分析和解决之路。

一、分析源码,寻找crash源头

step1:targetsdk为26 Activity的onCreate方法源码

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");
    }
}

从26的源码中可看出,当targetsdk>26,设置了屏幕方向,并且是透明的会报上述错误。证明在Android8.0手机上应用如果是targetsdk>26,设置了透明和屏幕方向则会挂掉

step2:targetsdk为27 Activity的onCreate方法源码

if (getApplicationInfo().targetSdkVersion >= O_MR1 && 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");
 	}
}

从27的源码中可看出,当targetsdk>=27,设置了屏幕方向,并且是透明的会报上述错误。证明在Android8.1手机上应用如果是targetsdk>=27,设置了透明和屏幕方向则会挂掉

step3:targetsdk为28 时,去掉了设置方向和透明冲突crash代码

targetsdk为28 Activity的onCreate方法源码中去掉了上述代码,不做限制。

step4:解释透明条件和固定方向

具体的什么是透明呢,看源码isTranslucentOrFloating,符合以下条件:

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

什么是固定方向呢?

/**
* Returns true if the activity's orientation is fixed.
* @hide
*/
public boolean isFixedOrientation() {
   
    return isFixedOrientationLandscape() || isFixedOrientationPortrait()
            || screenOrientation == SCREEN_ORIENTATION_LOCKED;
}

满足以下条件,就是满足固定方向

 orientation == SCREEN_ORIENTATION_PORTRAIT1|| orientation == SCREEN_ORIENTATION_SENSOR_PORTRAIT7|| orientation == SCREEN_ORIENTATION_REVERSE_PORTRAIT9|| orientation == SCREEN_ORIENTATION_USER_PORTRAIT;12)
 orientation == SCREEN_ORIENTATION_LANDSCAPE0|| orientation == SCREEN_ORIENTATION_SENSOR_LANDSCAPE
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值