Prevent screen rotation on Android
I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
将android:screenOrientation="portrait"
添加到清单或landscape
的<activity>
元素/ s,就完成了。
You can follow the logic below to prevent auto rotate screen while your AsyncTask
is running:
getRequestedOrientation()
. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
. AsyncTask
. AsyncTask
restore your previous orientation status using setRequestedOrientation(oldOrientation)
. Please note that there are several ways to access Activity
(which runs on UI thread) properties inside an AsyncTask
. You can implement your AsyncTask
as an inner class or you can use message Handler
that poke your Activiy
class.
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
链接地址: http://www.djcxy.com/p/8930.html
上一篇: 向下,行动
下一篇: 防止Android上的屏幕旋转