安卓:在活动暂时禁用方向更改 [英] Android: Temporarily disable orientation changes in an Activity

查看:267
本文介绍了安卓:在活动暂时禁用方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要活动有一定的code,使不应该被打断一些数据库的变化。我做繁重的另一个线程,并使用我设置为不可撤销的进度对话框。然而,我发现,如果我转我的手机将重新启动这是非常糟糕的正在运行的进程的活动,我也得到一个强制关闭。

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.

推荐答案

由于在他的<一个解释克里斯href="http://stackoverflow.com/questions/3611457/android-temporarily-disable-orientation-changes-in-an-activity/3611554#3611554">self-answer,呼叫

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

然后

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

真正的作品般的魅力......在真实设备上!

really works like charm... on real devices !

不要认为这是打破在模拟器上测试时,按Ctrl + F11快捷键随时更改屏幕方向,没有模拟传感器动作。

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.

编辑:这是不是最好的答案。作为评价所解释的,存在与这种方法的问题。 <一href="http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998">The真正的答案是这里。

this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.

这篇关于安卓:在活动暂时禁用方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆