我有一个我想阻止旋转的活动,因为我正在启动一个AsyncTask,屏幕旋转使它重新启动。
有没有办法告诉这个活动“即使用户像疯了一样摇晃他的手机也不要旋转屏幕”?
我有一个我想阻止旋转的活动,因为我正在启动一个AsyncTask,屏幕旋转使它重新启动。
有没有办法告诉这个活动“即使用户像疯了一样摇晃他的手机也不要旋转屏幕”?
当前回答
在AndroidManifest.xml文件中使用“portrait”用户似乎是一个很好的解决方案。但它会迫使某些设备(在横向环境下工作得最好)进入纵向模式,而不能获得正确的方向。在最新的Android版本上,你会得到一个错误。所以我的建议是使用“nosensor”。
<activity
...
...
android:screenOrientation="nosensor">
其他回答
在AndroidManifest.xml文件中使用“portrait”用户似乎是一个很好的解决方案。但它会迫使某些设备(在横向环境下工作得最好)进入纵向模式,而不能获得正确的方向。在最新的Android版本上,你会得到一个错误。所以我的建议是使用“nosensor”。
<activity
...
...
android:screenOrientation="nosensor">
Activity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="QRCodeActivity" android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
将以下内容添加到您的AndroidManifest.xml
[应用程序> src > main > AndroidManifest.xml]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
例子:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
android: screenOrientation =“肖像” 在应用程序标签<application
and
<活动 ... ... android: screenOrientation = "锁定" >
在AndroidManifest.xml中,ACTIVITY的以下属性就是你所需要的:
android:configChanges="orientation"
因此,完整的活动节点将是:
<activity android:name="Activity1"
android:icon="@drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>