Java 如何检查自动旋转屏幕设置是否在 Android 4.0+ 中打开/关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20902775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:10:13  来源:igfitidea点击:

How to check if auto-rotate screen setting is ON/OFF in Android 4.0+

javaandroid

提问by KennyPowers

I think each android device has an abitily to on/off auto-rotating function. Usually you can find it in settings->display->auto-rotate on/off. How can I read this setting state from my application? How can I access to this setting value? If you can share a code snipped i'd be very appreciate it.

我认为每个 android 设备都有一个能够打开/关闭自动旋转功能的能力。通常你可以在settings->display->auto-rotate on/off. 如何从我的应用程序中读取此设置状态?如何访问此设置值?如果你能分享一个剪断的代码,我会非常感激。

采纳答案by Harshal Benake

Hope this code snippet helps you out:-

希望此代码片段可以帮助您:-

@Override      
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);
    if (android.provider.Settings.System.getInt(getContentResolver(),
            Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
        Toast.makeText(getApplicationContext(), "Rotation ON", Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(getApplicationContext(), "Rotation OFF", Toast.LENGTH_SHORT).show();
    }
    super.onCreate(savedInstanceState);
}

回答by Vamshi

Try this:

尝试这个:

 public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
        {
              Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
        }

回答by Meenal

Use the following code:

使用以下代码:

if (android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
    Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(Rotation.this, "Rotation OFF", Toast.LENGTH_SHORT).show();
}