Android:如何迫使整个Android设备仅在景观或肖像上运行



有问题迫使设备锁定为景观/肖像模式?

我创建了一个启动器应用程序,该活动本身被为屏幕方向分配为景观,因为我的设备是唯一的景观设备。

进一步锁定屏幕,以便在选择和运行的其他应用程序将在景观屏幕中运行。

View orientationChanger = new LinearLayout(this);
    WindowManager.LayoutParams orientationLayout = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.RGBA_8888);
    orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    WindowManager wm = (WindowManager) this.getSystemService(Service.WINDOW_SERVICE);
    wm.addView(orientationChanger, orientationLayout);
    orientationChanger.setVisibility(View.VISIBLE);

它确实可以做技巧,例如

现在,我想允许用户在肖像模式下运行的某些应用程序旋转方向。

I tried to run these command on adb shell before doing it on my code:
try {
        Process stopAccelerometer = Runtime.getRuntime().exec("content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0");
        Process toProtrait = Runtime.getRuntime().exec("content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:3");
    } catch (IOException ex) {
        ex.printStackTrace();
    }

是的,它在肖像中运行了应用程序,但是如果我应用命令回到景观中,它的确会回到景观中。

            Process toProtrait = Runtime.getRuntime().exec("content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:0");

在代码中,最后一个值为0作为景观。我认为该应用程序本身不支持景观

我在网上找到了一个正在这样做的应用程序。 方向应用

请求活动到景观模式

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

请求肖像模式的活动

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

将其放在您的onCreate();

或只是将其设置在您的清单活动中

<activity
    android:screenOrientation="portrait/landscape"
    android:name=".ui.MainActivity"
    android:windowSoftInputMode="stateHidden"/>

最新更新