2016-11-29 75 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

相關問題