实现效果:编译开机,屏幕不会随着机器旋转而旋转
方法一:(成功)
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