找出设备方向是否锁定(检测自动旋转是否启用)
问题描述:
如何找出设备的屏幕方向是否锁定?我正在使用OrientationEventListener触发我的应用程序中的某些操作,如果用户锁定了他的屏幕,我想禁用它。找出设备方向是否锁定(检测自动旋转是否启用)
我知道我可以平时可定向喜欢这一点,但如何找出是这样的锁定方位:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// It's portrait
} else {
// It's landscape
}
答
使用此:
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();
}
答
您可以检查方向运行时,如:
@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();
}
}
希望它有帮助。
谢谢克里斯托,但这并不给信息是屏幕锁定。 – Astagron
也许我错过了你的问题,因为它不是很清楚你需要什么。我以为你想检查方向的类型。无论如何快乐的编码。 – Kristo