lock android screen orientation to landscape

不打扰是莪最后的温柔 提交于 2019-12-04 05:13:04

问题


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 . 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


回答1:


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. Specifically, the ACCELEROMETER_ROTATION and USER_ROTATION values will probably be of interest.

You will also need to declare the android.permission.WRITE_SETTINGS and possibly the android.permission.WRITE_SECURE_SETTINGS permissions in your manifest.



来源:https://stackoverflow.com/questions/12266315/lock-android-screen-orientation-to-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!