2016-11-29 76 views
0

有问题强制设备锁定为横向/纵向模式?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作为横向。我认为应用程序本身不支持风景,因此当我将应用程序应用于肖像时,该应用程序仍在运行。

我在网上找到了一个正在做这件事的应用程序。 Orientation Applications

回答

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"/> 
+0

我不能有斜杠screenOrientation设置。它只允许一个选项。 – LittleFunny

+0

yup ..它只是一个选项potrait或风景..选择一个.. – ZeroOne

相关问题