美文网首页
Only fullscreen activities can r

Only fullscreen activities can r

作者: 不正就是歪573 | 来源:发表于2019-04-24 18:08 被阅读0次

    Only fullscreen activities can request orientation终极解决方法

    import android.app.Activity;
    import android.content.pm.ActivityInfo;
    import android.content.res.TypedArray;
    import android.os.Build;
    
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    @Aspect
    public class FixOrientationCrash {
        private static final String TAG = FixOrientationCrash.class.getSimpleName();
    
        @Around("execution(* android.app.Activity+.setRequestedOrientation(..))")
        public Object fixThridCrash(ProceedingJoinPoint joinPoint) {
            Object object = null;
            try {
                if (Build.VERSION.SDK_INT == 26 && joinPoint.getTarget() instanceof Activity && this.isTranslucentOrFloating((Activity) joinPoint.getTarget())) {
                    boolean result = this.fixOrientation((Activity) joinPoint.getTarget());
                    LogUtils.i(TAG, "onCreate fixOrientation when Oreo, result = " + result, new Object[0]);
                } else {
                    object = joinPoint.proceed();
                }
            } catch (Throwable var4) {
                var4.printStackTrace();
            }
    
            return object;
        }
    
        @Around("execution(* android.app.Activity+.onCreate(..))")
        public Object fixSourceCrash(ProceedingJoinPoint joinPoint) {
            Object object = null;
            try {
                if (Build.VERSION.SDK_INT == 26 && joinPoint.getTarget() instanceof Activity && this.isTranslucentOrFloating((Activity) joinPoint.getTarget())) {
                    boolean result = this.fixOrientation((Activity) joinPoint.getTarget());
                    LogUtils.i(TAG, "onCreate fixOrientation when Oreo, result = " + result, new Object[0]);
                }
            } catch (Throwable var4) {
                var4.printStackTrace();
            }
    
            try {
                object = joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
            return object;
        }
    
        private boolean isTranslucentOrFloating(Activity activity) {
            boolean isTranslucentOrFloating = false;
    
            try {
                int[] styleableRes = (int[]) ((int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get((Object) null));
                TypedArray ta = activity.obtainStyledAttributes(styleableRes);
                Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
                m.setAccessible(true);
                isTranslucentOrFloating = (Boolean) m.invoke((Object) null, ta);
                m.setAccessible(false);
            } catch (Exception var5) {
                LogUtils.e(TAG, "isTranslucentOrFloating occur error: " + var5.getMessage(), new Object[0]);
            }
    
            return isTranslucentOrFloating;
        }
    
        private boolean fixOrientation(Activity activity) {
            try {
                Field field = Activity.class.getDeclaredField("mActivityInfo");
                field.setAccessible(true);
                ActivityInfo o = (ActivityInfo) field.get(activity);
                o.screenOrientation = -1;
                field.setAccessible(false);
                return true;
            } catch (Exception var3) {
                LogUtils.e(TAG, "fixOrientation occur error: " + var3.getMessage(), new Object[0]);
                return false;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Only fullscreen activities can r

          本文链接:https://www.haomeiwen.com/subject/hhkngqtx.html