Android-防止屏幕旋转问题

我创建了一个AsyncTask任务,屏幕一旋转它就重新启动。所以又创建了一个活动想达到防止屏幕旋转的功能。那么有什么方法能来告诉这个活动“即使用户怎么摇动他的手机,屏幕都不会旋转呢?”

在manifest或landscape中添加android:screenOrientation="portrait",就能达到你想要的功能。

 @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) {       
     }   

 <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>