android 7.0 关闭系统自动旋转

实现效果:编译开机,屏幕不会随着机器旋转而旋转

方法一:(成功)

1、点击界面 DisplaySettings.java

下面这段代码就是为了显示出setting 里面 旋转按钮的点击界面


        if (RotationPolicy.isRotationLockToggleVisible(activity)) {
            DropDownPreference rotatePreference =
                    (DropDownPreference) findPreference(KEY_AUTO_ROTATE);
            int rotateLockedResourceId;
            // The following block sets the string used when rotation is locked.
            // If the device locks specifically to portrait or landscape (rather than current
            // rotation), then we use a different string to include this information.
            if (allowAllRotations(activity)) {
                rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current;
            } else {
                if (RotationPolicy.getRotationLockOrientation(activity)
                        == Configuration.ORIENTATION_PORTRAIT) {
                    rotateLockedResourceId =
                            R.string.display_auto_rotate_stay_in_portrait;
                } else {
                    rotateLockedResourceId =
                            R.string.display_auto_rotate_stay_in_landscape;
                }
            }
            rotatePreference.setEntries(new CharSequence[] {
                    activity.getString(R.string.display_auto_rotate_rotate),
                    activity.getString(rotateLockedResourceId),
            });
            rotatePreference.setEntryValues(new CharSequence[] { "0", "1" });
            rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(activity) ?
                    1 : 0);
            rotatePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    final boolean locked = Integer.parseInt((String) newValue) != 0;
                    MetricsLogger.action(getActivity(), MetricsEvent.ACTION_ROTATION_LOCK,
                            locked);
                    RotationPolicy.setRotationLock(activity, locked);
                    return true;
                }
            });
        } else {
            removePreference(KEY_AUTO_ROTATE);
        }

2、跳转到 RotationPolicy.java (修改文件)

    /**
     * Returns true if rotation lock is enabled.
     */
    public static boolean isRotationLocked(Context context) {
        return Settings.System.getIntForUser(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
    }

将这个代码修改为:

/**
     * Returns true if rotation lock is enabled.
     */
    public static boolean isRotationLocked(Context context) {
        setRotationLock(true, NATURAL_ROTATION);
        return true;
    }

就是在它作出判断书不是自动旋转的时候,让它跳转到锁定自动旋转函数

private static void setRotationLock(final boolean enabled, final int rotation) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                    if (enabled) {
                    Log.w(TAG, "lum_1 rotation is :" + rotation);
                        wm.freezeRotation(rotation); (禁止自动旋转)
                    } else {
                    Log.w(TAG, "lum_2 rotation is " + rotation);

                      wm.thawRotation(); (可以自动旋转)
                    }
                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    }

将自动旋转注销即可。

修改为:

   private static void setRotationLock(final boolean enabled, final int rotation) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = WindowManagerGlobal.getWindowManagerService();

                        wm.freezeRotation(rotation);

                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    }
方法2 :(没成功)

frameworks/base/cmds/screencap/screencap.cpp

修改:

    uint8_t displayOrientation = configs[activeConfig].orientation;
    +        displayOrientation = ISurfaceComposer::eRotateNone;
    uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];

文献参考:
辅助设置中自动旋转
http://blog.csdn.net/wangxueming/article/details/43969341

强制系统横屏竖屏
http://blog.csdn.net/kongbaidepao/article/details/54427625

Android6.0 旋转屏幕(四)应用强制设置方向
http://blog.csdn.net/kc58236582/article/details/53741445

关于 Android 4.4 系统屏幕旋转调研 (推荐)
http://blog.csdn.net/jangel_lee/article/details/44949155

android如何改变系统默认横竖屏方向
http://blog.csdn.net/wh_19910525/article/details/37963375

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值