简体   繁体   English

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

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

I think each android device has an abitily to on/off auto-rotating function.我认为每个 android 设备都有一个能够打开/关闭自动旋转功能的能力。 Usually you can find it in settings->display->auto-rotate on/off .通常你可以在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.如果你能分享一个剪断的代码,我会非常感激。

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);
}

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();
}

Try this:试试这个:

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

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

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