原因:
出現(xiàn)Only fullscreen opaque activities can request orientation這個(gè)問(wèn)題是因?yàn)?.0之前全屏不透明活動(dòng)不可以請(qǐng)求方向 要不然只能設(shè)置不透明煮盼,你看看這扯淡不,我要是不設(shè)置透明我還會(huì)碰見(jiàn)這種情況带污? 那么有沒(méi)有辦法解決呢僵控? 那肯定是有的
解決方案:
優(yōu)雅的方式:Hook反射繞過(guò)檢查
聽(tīng)起來(lái)是不是很cool
思路:
判斷是否透明色或者懸浮
修改activity的屏幕方向,不固定鱼冀,繞過(guò)檢查报破。
public class ActivityHook {
/**
* java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
* <p>
* 修復(fù)android 8.0存在的問(wèn)題
* <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è)置屏幕不固定,繞過(guò)檢查
*
* @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千绪,繞過(guò)檢查
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxxx);
}
試一下充易,是不是問(wèn)題就解決了呢 尿啊~~
大家快去解決問(wèn)題吧!