Find out if device orientation is locked (Detect if auto-rotate is enabled/disabled)

半腔热情 提交于 2019-12-23 15:32:47

问题


How can I find out is device's screen orientation locked? I am using OrientationEventListener to trigger some actions inside my app which I would like to disable if user has his screen locked.

I know I can usually can orientation like this but how to find out is this locked orientation:

    int orientation = getResources().getConfiguration().orientation;

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        // It's portrait
    } else {
        // It's landscape
    }

回答1:


Use this:

if (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
        Toast.makeText(getApplicationContext(), "Auto Rotate is ON", Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(getApplicationContext(), "Auto Rotate is OFF", Toast.LENGTH_SHORT).show();
    }



回答2:


You can check the orientation in runtime like:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();        
}
}

Hope it helps.



来源:https://stackoverflow.com/questions/34636464/find-out-if-device-orientation-is-locked-detect-if-auto-rotate-is-enabled-disab

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