Android:暂时禁用活动中的方向更改
我的主要活动有一些代码,这些代码进行了一些不应中断的数据库更改。我正在另一个线程中完成繁重的工作,并使用我设置为不可取消的进度对话框。但是,我注意到,如果我旋转手机,它会重新启动活动,这对于正在运行的进程来说确实很糟糕,并且我得到了强制关闭。
我想要做的是以编程方式禁用屏幕方向更改,直到我的进程完成,此时方向更改将启用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
正如克里斯在他的自我回答,打电话
,然后
真的像魅力一样工作......在真实设备上!
不要认为在模拟器上测试时它已损坏,ctrl+F11 快捷键始终会更改屏幕方向,而不模拟传感器移动。
编辑:这不是最好的答案。正如评论中所解释的,这种方法存在问题。 真正的答案在这里< /a>.
As explained by Chris in his self-answer, calling
and then
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.
EDIT: this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.
其他答案都不能完美地满足我的要求,但我发现这确实有效。
将方向锁定为当前...
当应再次允许更改方向时,设置回默认值...
None of the other answers did the trick perfectly for me, but here's what I found that does.
Lock orientation to current...
When changing orientation should be allowed again, set back to default...
这是一个更完整和最新的解决方案,适用于 API 8+,适用于反向纵向和横向,并适用于“自然”方向为横向的 Galaxy 选项卡(调用
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED )
解锁方向):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):使用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
进行锁定当前方向,无论是横向还是纵向。
使用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
解锁方向。Use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
forlocking current orientation whether it be landscape or portrait.
Use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
for unlocking orientation.为了管理反向方向模式,我使用该代码来修复活动方向:
并再次允许方向:
In order to manage also the reverse orientation modes, I have used that code to fix the activity orientation:
And to allow again the orientation:
我找到了答案。为此,您可以在 Activity 中使用此处指定的值之一调用
setRequestedOrientation(int)
:http://developer.android.com/reference/android/R.attr.html#screenOrientation在我启动线程之前,我调用了
setRequestedOrientation( OFF)
(OFF = nosensor),当线程完成时,我调用setRequestedOrientation(ON)
(ON =sensor)。就像魅力一样。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#screenOrientationBefore I kicked off my thread I called
setRequestedOrientation(OFF)
(OFF = nosensor) and when the thread was done I calledsetRequestedOrientation(ON)
(ON = sensor). Works like a charm.谢谢大家。我修改了Pilot_51的解决方案,以确保恢复到之前的状态。我还进行了更改以支持非横向和非纵向屏幕(但尚未在此类屏幕上进行测试)。
然后恢复它
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).
Then to restore it
这是一种每次都有效并保留当前方向的解决方案(例如使用
Activity.Info.SCREEN_ORIENTATION_PORTRAIT
设置为 0°,但用户可以将 180° 方向作为当前方向)。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).如果您只想在设备上启用屏幕时旋转屏幕,请使用
ActivityInfo.SCREEN_ORIENTATION_USER
。use
ActivityInfo.SCREEN_ORIENTATION_USER
if you want to rotate screen only if its enabled on device.这对我来说非常有效。它解决了平板电脑/手机不同“自然方向”的问题;)
This works prefect for me. It solves problem with different "natural orientation" of tablet/phone ;)
我提出了一个解决方案,该解决方案取决于显示旋转,然后决定设备的方向。通过了解方向,我们可以锁定方向并在需要时释放它。该解决方案还可以确定设备是否处于反向横向模式。
然后如果我们需要释放方向,我们可以调用这个方法:
I have come up with a solution which depends on the display rotation and then decides the orientation of the device. From knowing the orientation we can lock the orientation and release it later when needed. This solution also can determine if the device in reverse landscape mode.
Then later if we need to release the orientation we can call this method:
我认为这段代码更容易阅读。
I think this code is easier to read.
我发现需要组合现有的旋转/方向值来涵盖四种可能性;有纵向/横向值和设备的自然方向。假设当屏幕处于“自然”纵向或横向时,设备的自然方向的旋转值为 0 度。同样,当处于横向或纵向时,会有 90 度的旋转值(注意它与 0 度方向相反)。因此,非 0 度或 90 度的旋转值将意味着“反向”方向。好的,这是一些代码:
I have found a combination of existing rotation/orientation values are needed to cover the four possibilities; there's the portrait/landscape values and the natural orientation of the device. Let's say the devices' natural orientation will have a rotation value of 0 degrees when the screen is in it's "natural" portrait or landscape orientation. Similarly, there will be a rotation value of 90 degrees when it's in landscape or portrait (notice it's opposite of the orientation @ 0 degrees). So the rotation values that are not 0 or 90 degrees will imply "Reverse" orientation. Ok, here's some code:
我不喜欢这里的大多数答案,因为在解锁中他们将其设置为“未指定”而不是之前的状态。 ProjectJourneyman 确实考虑到了这一点,这很棒,但我更喜欢 Roy 的锁定代码。因此,我的建议是两者的结合:
I didn't like most of the answers here, since in the unlock they set it to UNSPECIFIED as opposed to the previous state. ProjectJourneyman did take it into account, which was great, but I preferred the locking code by Roy. So, my recommendation would be a mix of the two:
您可以使用
You can use
我想仅在特定屏幕/片段上停止旋转,并最终得到了基于上面 Ivan BASART 工作 的代码,
I wanted to stop rotation just on a specific screen/fragment and ended up with this code based on Ivan BASART work above,
使用该行代码
在您的活动 oncreate 方法中
use that line of code
in ur activity oncreate method