Android9.0或11.0屏幕旋转值保存

Android9.0和11.0系统开机之后发现屏幕会自动转到0度,可以自己保存一下这个旋转值,在初始化时旋转到保存的角度。

diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
index c2fcb28d7a4c..a192ce8cceb8 100755
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
@@ -1457,12 +1457,16 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback
                        Log.d(TAG, "getRotation before = " + pre_Rotation);
                        if (pre_Rotation == Surface.ROTATION_0){
                                mWindowManagerService.freezeRotation(Surface.ROTATION_90);
+                               SystemProperties.set("persist.vendor.orientation", "90");
             }else if (pre_Rotation == Surface.ROTATION_90){
                                mWindowManagerService.freezeRotation(Surface.ROTATION_180);
+                               SystemProperties.set("persist.vendor.orientation", "180");
             }else if (pre_Rotation == Surface.ROTATION_180){
                                mWindowManagerService.freezeRotation(Surface.ROTATION_270);
+                               SystemProperties.set("persist.vendor.orientation", "270");
             }else{
                                mWindowManagerService.freezeRotation(Surface.ROTATION_0);
+                               SystemProperties.set("persist.vendor.orientation", "0");
                        }
                } catch (RemoteException e) {
                        Log.d(TAG, "onRotateKeyClick error:" + e.toString());
@@ -1772,6 +1776,7 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback
                        else if("com.itemp.orierntion.changer".equals(action)){
                                String orierntion = intent.getExtras().getString("orierntion");
                                int newOrierntion = TextUtils.isEmpty(orierntion) ? 0 : Integer.valueOf(orierntion.trim())/90;
+                               SystemProperties.set("persist.vendor.orientation", orierntion);
                                try {
                     mWindowManagerService.freezeRotation(newOrierntion);
                 }catch(RemoteException e){
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
old mode 100644
new mode 100755
index 1f368e164678..6aaaaa21995a
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RotationLockControllerImpl.java
@@ -22,7 +22,7 @@ import android.os.UserHandle;
 import com.android.internal.view.RotationPolicy;
 
 import java.util.concurrent.CopyOnWriteArrayList;
-
+import android.util.Log;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 
@@ -30,6 +30,7 @@ import javax.inject.Singleton;
 @Singleton
 public final class RotationLockControllerImpl implements RotationLockController {
     private final Context mContext;
+       private static final String ROTATE_SCREEN_KEY = "persist.vendor.orientation";//"persist.sys.user_rotation";
     private final CopyOnWriteArrayList<RotationLockControllerCallback> mCallbacks =
             new CopyOnWriteArrayList<RotationLockControllerCallback>();
 
@@ -69,7 +70,11 @@ public final class RotationLockControllerImpl implements RotationLockController
     }
 
     public void setRotationLockedAtAngle(boolean locked, int rotation){
-        RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
+        //RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation);
+               int rotation_screen = android.os.SystemProperties.getInt(ROTATE_SCREEN_KEY, 0);
+               rotation_screen /= 90;
+        //Log.d("Rotation", "setRotationLockedAtAngle rotation_screen = " + rotation_screen + " , locked = " + locked);
+        RotationPolicy.setRotationLockAtAngle(mContext, locked, rotation_screen);
     }
 
     public boolean isRotationLockAffordanceVisible() {
(END)

调试命令:

settings get system accelerometer_rotation
settings put system accelerometer_rotation 0 解锁屏幕旋转
settings put system accelerometer_rotation 1 锁定屏幕旋转
settings get system user_rotation
settings put system user_rotation 1 旋转到90

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 12 平板修改了 user_rotation 参数时,会导致屏幕旋转方向的改变,但是应用程序的界面可能还停留在旋转之前的方向,这会导致应用程序界面的显示不正确。为了解决这个问题,你可以在应用程序中监听系统的旋转事件,并相应地调整应用程序界面的显示方向。 具体来说,你可以使用以下代码来监听系统的旋转事件: ```java public class MyActivity extends Activity { private RotationEventListener mRotationEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRotationEventListener = new RotationEventListener(this); mRotationEventListener.enable(); } @Override protected void onDestroy() { super.onDestroy(); mRotationEventListener.disable(); } private class RotationEventListener extends OrientationEventListener { private int mLastRotation = -1; public RotationEventListener(Context context) { super(context); } @Override public void onOrientationChanged(int orientation) { int rotation = getWindowManager().getDefaultDisplay().getRotation(); if (mLastRotation != rotation) { mLastRotation = rotation; adjustLayout(); } } } private void adjustLayout() { int rotation = getWindowManager().getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case Surface.ROTATION_90: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; case Surface.ROTATION_180: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); break; case Surface.ROTATION_270: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; } } } ``` 这段代码中,我们首先创建一个 RotationEventListener,用于监听系统的旋转事件。当系统的旋转方向发生变化时,我们调用 adjustLayout 方法,根据当前的旋转方向来调整应用程序界面的显示方向。 在 adjustLayout 方法中,我们首先获取当前的旋转方向,然后根据旋转方向来设置应用程序的显示方向。例如,当旋转方向为 0 时,我们将应用程序的显示方向设置为竖屏模式,当旋转方向为 90 时,我们将应用程序的显示方向设置为横屏模式,以此类推。 通过这种方式,我们就能够在 Android 12 平板修改 user_rotation 参数时,相应地调整应用程序界面的显示方向,以确保应用程序的界面能够正确地显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值