我正在使用以下代码在用户旋转设备时更改我的活动方向。这第一次工作正常,但没有再次调用。请纠正我。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("ACTIVITY","CHANGING ORIENTATION"); //No i18n
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.d(TAG, "ORIENTATION CHANGED TO PORTRAIT"); // No I18N
setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d(TAG, "ORIENTATION CHANGED TO LANDSCAPE"); // No I18N
setRequestedOrientation(Configuration.ORIENTATION_LANDSCAPE);
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d("ACTIVITY","ON RESTORE INSTANCESTATE"); //No i18n
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.d("ACTIVITY","ON SAVE INSTANCESTATE"); //No i18n
super.onSaveInstanceState(outState);
}
答案 0 :(得分:2)
使用setRequestedOrientation(...);
后,您将无法获得轮换通知的权限
如果要在旋转发生时执行特殊操作,可能需要使用OrientationEventListener
修改强>
注意:此代码将纵向视为80-90度和> = 320,这可以更改
如果这段代码没有在那里运行,你可能还需要在UI线程上运行setRequestedOrientation。
你应该这样做:
mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
@Override
public void onOrientationChanged(int rotation) {
boolean changed = false;
if (((rotation >= 0) && (rotation <= 80)) || (rotation >= 320)) {
setRequestedOrientation(Configuration.ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(Configuration.ORIENTATION_LANDSCAPE);
}
}
};
mOrientationListener.enable();