默认开机横屏(全网最简单,不影响重力传感器)

默认开机横屏

很多老人机和平板都有默认系统默认开机横屏的需求,但是网上修改大多都比较麻烦且固定死了一个方向,本文将介绍一种较为简单的实现方式(这种方式不影响开机动画)。

实现原理:系统自动旋转其实是修改的是Settings.System.USER_ROTATION这个值,我们只要在系统启动时将这个值修改为我们需要角度即可,接下来将介绍在Android7和Android9的实现方式。

Android7实现方式:

将自动旋转默认关闭

frameworks/base/packages/SettingsProvider/res/values/defaults.xml

-    <bool name="def_accelerometer_rotation">true</bool>
+    <bool name="def_accelerometer_rotation">false</bool>

-    <integer name="def_user_rotation">0</integer>
+    <integer name="def_user_rotation">90</integer>

默认角度修改为自己需要

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

import com.mediatek.multiwindow.MultiWindowManager;
-
+import android.view.Surface;
 public final class ActivityManagerService extends ActivityManagerNative
         implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
 
@@ -4122,6 +4122,7 @@ public final class ActivityManagerService extends ActivityManagerNative
             // error message and don't try to start anything.
             return false;
         }
+               Settings.System.putInt(mContext.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_90);
         Intent intent = getHomeIntent();
         ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
         if (aInfo != null) {

将Launcher设置为默认允许旋转

packages/apps/Launcher3/src/com/android/launcher3/Utilities.java

@ -143,16 +143,16 @@ public final class Utilities {
     }
 
     public static boolean isAllowRotationPrefEnabled(Context context) {
-        boolean allowRotationPref = false;
-        if (ATLEAST_N) {
-            // If the device was scaled, used the original dimensions to determine if rotation
-            // is allowed of not.
-            int originalDensity = DisplayMetrics.DENSITY_DEVICE_STABLE;
-            Resources res = context.getResources();
-            int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
-                    * res.getDisplayMetrics().densityDpi / originalDensity;
-            allowRotationPref = originalSmallestWidth >= 600;
-        }
+        boolean allowRotationPref = true;
+        // if (ATLEAST_N) {
+        //     // If the device was scaled, used the original dimensions to determine if rotation
+        //     // is allowed of not.
+        //     int originalDensity = DisplayMetrics.DENSITY_DEVICE_STABLE;
+        //     Resources res = context.getResources();
+        //     int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
+        //             * res.getDisplayMetrics().densityDpi / originalDensity;
+        //     allowRotationPref = originalSmallestWidth >= 600;
+        // }
         return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, allowRotationPref);
     }

锁屏允许旋转

frameworks/base/core/res/res/values/config.xml

-    <bool name="config_enableLockScreenRotation">false</bool>
+    <bool name="config_enableLockScreenRotation">true</bool>

Android9实现方式:

开机修改为自己要的角度

frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

import com.android.server.vr.VrManagerInternal;
 import com.android.server.wm.PinnedStackWindowController;
 import com.android.server.wm.WindowManagerService;
-
+import android.view.Surface;
 /// M: MTK AmsExt
 import com.mediatek.server.am.AmsExt;
 
@@ -4751,6 +4751,9 @@ public class ActivityManagerService extends IActivityManager.Stub
             // error message and don't try to start anything.
             return false;
         }
+               
+               Settings.System.putInt(mContext.getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_90);
+               
         Intent intent = getHomeIntent();
         ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
         if (aInfo != null) {

Launcher默认可旋转

index e866445..ff4b560 100644
--- a/ap/vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/states/RotationHelper.java
+++ b/ap/vendor/mediatek/proprietary/packages/apps/Launcher3/src/com/android/launcher3/states/RotationHelper.java
@@ -38,15 +38,15 @@ public class RotationHelper implements OnSharedPreferenceChangeListener {
     public static final String ALLOW_ROTATION_PREFERENCE_KEY = "pref_allowRotation";
 
     public static boolean getAllowRotationDefaultValue() {
-        if (ATLEAST_NOUGAT) {
-            // If the device was scaled, used the original dimensions to determine if rotation
-            // is allowed of not.
-            Resources res = Resources.getSystem();
-            int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
-                    * res.getDisplayMetrics().densityDpi / DENSITY_DEVICE_STABLE;
-            return originalSmallestWidth >= 600;
-        }
-        return false;
+        // if (ATLEAST_NOUGAT) {
+        //     // If the device was scaled, used the original dimensions to determine if rotation
+        //     // is allowed of not.
+        //     Resources res = Resources.getSystem();
+        //     int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
+        //             * res.getDisplayMetrics().densityDpi / DENSITY_DEVICE_STABLE;
+        //     return originalSmallestWidth >= 600;
+        // }
+        return true;
     }

默认关闭自动自动旋转、修改默认角度

--- a/ap/vendor/mediatek/proprietary/packages/apps/SettingsProvider/res/values/defaults.xml
+++ b/ap/vendor/mediatek/proprietary/packages/apps/SettingsProvider/res/values/defaults.xml
@@ -86,7 +86,7 @@
     <integer name="def_max_sound_trigger_detection_service_ops_per_day" translatable="false">1000</integer>
     <integer name="def_sound_trigger_detection_service_op_timeout" translatable="false">15000</integer>
 
-    <bool name="def_lockscreen_disabled">false</bool>
+    <bool name="def_lockscreen_disabled">true</bool>
     <bool name="def_device_provisioned">false</bool>
     <integer name="def_dock_audio_media_enabled">1</integer>
 
@@ -115,7 +115,7 @@
     <bool name="def_accessibility_display_magnification_auto_update">true</bool>
 
     <!-- Default for Settings.System.USER_ROTATION -->
-    <integer name="def_user_rotation">0</integer>
+    <integer name="def_user_rotation">90</integer>
 
     <!-- Default for Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE. <=0 if no limit -->

每次关机都会被SystemUI改掉,所以需要添加以下修改

diff --git a/ap/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java b/ap/vendor/mediatek/proprietary/packa
ges/apps/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
old mode 100644
new mode 100755
index 5418dc1..7c87e51
--- a/ap/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
+++ b/ap/vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
@@ -64,7 +64,7 @@ public final class RotationLockControllerImpl implements RotationLockController
     }
 
     public void setRotationLockedAtAngle(boolean locked, int rotation){
-        RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
+        RotationPolicy.setRotationLockAtAngle(mContext, locked, 1);
     }
 
     public boolean isRotationLockAffordanceVisible() {

至此,开机默认横屏简单方法介绍完毕!如有不清楚的可留言

相关推荐
阿洵Rain4 小时前
【Linux】环境变量
android·linux·javascript
hong1616886 小时前
PhpStorm中配置调试功能
android·ide·phpstorm
DongGei8 小时前
安卓-音频焦点
android·微信·音视频
冬田里的一把火38 小时前
[Android] [SnapdragonCamera] 单摄(横屏)阶段总结
android·数码相机
夏目艾拉9 小时前
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
android·java·flutter·设计模式
似霰9 小时前
安卓源码libvulkan“ depends on undefined module “libgpud_sys“报错解决
android
Arms20611 小时前
Android Fragment 学习备忘
android·学习
落魄的Android开发11 小时前
Android 跳转至各大应用商店应用详情页
android
技术无疆11 小时前
DDComponentForAndroid:探索Android组件化方案
android·java·开源·android-studio·组件化
繁依Fanyi12 小时前
【Python 千题 —— 算法篇】数字反转
android·java·开发语言·python·算法·eclipse·tomcat