简体   繁体   English

锁定Android屏幕方向到景观

[英]lock android screen orientation to landscape

I am developing an android application whose one feature is to lock screen orientation to Landscape , I want to apply this orientation change to all the android application in phone . 我正在开发一个Android应用程序,其中一个功能是将屏幕方向锁定到Landscape,我想将此方向更改应用于手机中的所有Android应用程序。 I am using this code 我正在使用此代码

private void lockScreenOrientation() {
if (!mScreenOrientationLocked) {
    final int orientation = getResources().getConfiguration().orientation;
    final int rotation = getWindowManager().getDefaultDisplay().getOrientation();
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }
    else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        }
        else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }

    mScreenOrientationLocked = true;
}
}

private void unlockScreenOrientation() {
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
 mScreenOrientationLocked = false;
}

But this is temporary change , doesnt take effect to all application , does anyone know a way to apply lock orientation to all applications ? 但这是暂时的改变,并没有对所有应用程序生效,有没有人知道一种方法将锁定方向应用于所有应用程序? Thank you 谢谢

Applications like the one you have linked do this by modifying the global system settings values associated with rotation. 您链接的应用程序通过修改与旋转关联的全局系统设置值来执行此操作。 Have a look at the Settings.System class for the constants available to applications. 查看Settings.System类,了解应用程序可用的常量。 Specifically, the ACCELEROMETER_ROTATION and USER_ROTATION values will probably be of interest. 具体来说, ACCELEROMETER_ROTATIONUSER_ROTATION值可能会感兴趣。

You will also need to declare the android.permission.WRITE_SETTINGS and possibly the android.permission.WRITE_SECURE_SETTINGS permissions in your manifest. 您还需要在清单中声明android.permission.WRITE_SETTINGS以及可能的android.permission.WRITE_SECURE_SETTINGS权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM