Android:暂时禁用活动中的方向更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3611457/
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
Android: Temporarily disable orientation changes in an Activity
提问by Christopher Perry
My main activity has some code that makes some database changes that should not be interrupted. I'm doing the heavy lifting in another thread, and using a progress dialog which I set as non-cancellable. However, I noticed that if I rotate my phone it restarts the activity which is REALLY bad for the process that was running, and I get a Force Close.
我的主要活动有一些代码可以进行一些不应中断的数据库更改。我正在另一个线程中执行繁重的工作,并使用我设置为不可取消的进度对话框。但是,我注意到,如果我旋转手机,它会重新启动对正在运行的进程非常不利的活动,并且我会收到强制关闭。
What I want to do is programatically disable screen orientation changes until my process completes, at which time orientation changes are enabled.
我想要做的是以编程方式禁用屏幕方向更改,直到我的过程完成,此时启用方向更改。
回答by Kevin Gaudin
As explained by Chris in his self-answer, calling
正如克里斯在他的自我回答中所解释的那样,打电话
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
and then
进而
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
really works like charm... on real devices !
真的像魅力一样工作......在真实设备上!
Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.
在模拟器上测试时不要认为它坏了,ctrl+F11 快捷键总是改变屏幕方向,而不模拟传感器移动。
EDIT: this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.
编辑:这不是最好的答案。正如评论中所解释的,这种方法存在问题。真正的答案在这里。
回答by Pilot_51
None of the other answers did the trick perfectly for me, but here's what I found that does.
其他答案都没有完美地解决我的问题,但这是我发现的。
Lock orientation to current...
锁定方向到当前...
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
When changing orientation should be allowed again, set back to default...
当应再次允许更改方向时,请设置回默认值...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
回答by Roy
Here is a more complete and up to date solution that works for API 8+, works for reverse portrait and landscape, and works on a Galaxy tab where the "natural" orientation is landscape (call activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
to unlock the orientation):
这是一个更完整和最新的解决方案,适用于 API 8+,适用于反向纵向和横向,并适用于“自然”方向为横向的 Galaxy 选项卡(调用activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
解锁方向):
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void lockActivityOrientation(Activity activity) {
Display display = activity.getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
int height;
int width;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
height = display.getHeight();
width = display.getWidth();
} else {
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
}
switch (rotation) {
case Surface.ROTATION_90:
if (width > height)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_180:
if (height > width)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_270:
if (width > height)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default :
if (height > width)
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
回答by Ivan BASART
In order to manage also the reverse orientation modes, I have used that code to fix the activity orientation:
为了管理反向模式,我使用该代码来修复活动方向:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch(rotation) {
case Surface.ROTATION_180:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_0:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
And to allow again the orientation:
并再次允许方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
回答by Sudhir Sinha
Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
for
locking current orientation whether it be landscape or portrait.
使用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
用于锁定当前的方向,无论是横向还是纵向。
Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
for unlocking orientation.
使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
的解锁方向。
回答by Christopher Perry
I found the answer. To do this, in an Activity you can call setRequestedOrientation(int)
with one of the values specified here: http://developer.android.com/reference/android/R.attr.html#screenOrientation
我找到了答案。为此,在 Activity 中,您可以setRequestedOrientation(int)
使用此处指定的值之一进行调用:http: //developer.android.com/reference/android/R.attr.html#screenOrientation
Before I kicked off my thread I called setRequestedOrientation(OFF)
(OFF = nosensor) and when the thread was done I called setRequestedOrientation(ON)
(ON = sensor). Works like a charm.
在我开始我的线程之前,我调用了setRequestedOrientation(OFF)
(OFF = nosensor),线程完成后我调用了setRequestedOrientation(ON)
(ON = sensor)。奇迹般有效。
回答by ProjectJourneyman
Thanks all. I modified Pilot_51's solution, to make sure I restored to the previous state. I also threw in a change to support non-landscape and non-portrait screens (but haven't tested it on such a screen).
谢谢大家。我修改了 Pilot_51 的解决方案,以确保我恢复到以前的状态。我还进行了更改以支持非横向和非纵向屏幕(但尚未在此类屏幕上对其进行测试)。
prevOrientation = getRequestedOrientation();
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}
Then to restore it
然后恢复它
setRequestedOrientation(prevOrientation);
回答by tdjprog
protected void setLockScreenOrientation(boolean lock) {
if (Build.VERSION.SDK_INT >= 18) {
setRequestedOrientation(lock?ActivityInfo.SCREEN_ORIENTATION_LOCKED:ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
return;
}
if (lock) {
switch (getWindowManager().getDefaultDisplay().getRotation()) {
case 0: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; // value 1
case 2: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); break; // value 9
case 1: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; // value 0
case 3: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); break; // value 8
}
} else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); // value 10
}
回答by Adrien Cadet
Here is a solution which works every time and preserves current orientation (using Activity.Info.SCREEN_ORIENTATION_PORTRAIT
sets to 0° for instance, but user can have a 180° orientation as current one).
这是一个每次都有效并保留当前方向的解决方案(Activity.Info.SCREEN_ORIENTATION_PORTRAIT
例如,使用设置为 0°,但用户可以将 180° 方向作为当前方向)。
// Scope: Activity
private void _lockOrientation() {
if (super.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT);
} else {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
}
}
private void _unlockOrientation() {
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
回答by Pawan Maheshwari
use ActivityInfo.SCREEN_ORIENTATION_USER
if you want to rotate screen only if its enabled on device.
使用ActivityInfo.SCREEN_ORIENTATION_USER
,如果你想仅当设备启用其旋转屏幕。