如何使用android 4.x运行asyntask时处理屏幕方向更改

问题描述:

我试图实现以下代码来处理屏幕方向更改。如何使用android 4.x运行asyntask时处理屏幕方向更改

****DataBaseUpdateService.java**** 

public class DataBaseUpdateService extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Updatetask Task = new Updatetask(this.getApplicationContext()); 
      if(Task.getStatus() == AsyncTask.Status.PENDING){ 
      Task.execute();}; 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 
} 

=========================================== ===============================

****Androidmanifest.xml**** 
<activity 
    android:name=".DataBaseUpdateService" 
    android:configChanges="keyboardHidden|orientation"/> 

这些代码可以与android 3.x或更低版本完美配合使用。但是,它不适用于Android 4.x.

你有什么想法是什么问题?

解决方案1 ​​ - 如果您真的需要一个AsyncTask,请考虑两次。 解决方案2 - 将AsyncTask放入片段中。 解决方案3 - 锁定屏幕方向 解决方案4 - 防止重新创建活动。

参考:http://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/

..... 问题发生,因为活动时重新配置改变,如取向变化等 可以锁定在onPreExecuted()asyntask的取向变化的方法和解锁它在onPostExecuted()方法中。

import android.app.Activity; 
import android.content.Context; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.os.AsyncTask; 
import android.widget.Button; 
import android.widget.ProgressBar; 

public class MyAsyncTask extends AsyncTask<Void, Integer, Void> { 
private Context context; 
private ProgressBar progressBar; 
private Button button; 

public MyAsyncTask(ProgressBar progressBar,Context context,Button button){ 
    this.context=context; 
    this.progressBar=progressBar; 
    this.button=button; 

} 

private void lockScreenOrientation() { 
    int currentOrientation =context.getResources().getConfiguration().orientation; 
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) { 
     ((Activity) 
    context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } else { 
     ((Activity) context). 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 
} 

private void unlockScreenOrientation() { 
    ((Activity) context). 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    progressBar.setMax(100); 
    button.setEnabled(false); 
    lockScreenOrientation(); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    // TODO Auto-generated method stub 


    for(int i=0;i<=100;i++){ 
    try { 
     Thread.sleep(200); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    publishProgress(i); 

    } 

return null; 
} 
@Override 
protected void onProgressUpdate(Integer... values) { 
    // TODO Auto-generated method stub 
    super.onProgressUpdate(values); 
    progressBar.setProgress(values[0]); 
} 
@Override 
protected void onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    button.setEnabled(true); 
    unlockScreenOrientation(); 
} 




} 
+0

Thanks.worked给我.. – 2014-09-12 10:52:31

增加screenSize值。

documentation

注意:如果您的应用程序面向API级别13或更高(如通过中的minSdkVersion和targetSdkVersion属性声明),然后 你也应该申报screenSize配置,因为它 当设备在纵向和横向 方向之间切换时也会改变。

因此,清单应该像这样(如果你想处理方向更改自己):

****Androidmanifest.xml**** 
<activity 
    android:name=".DataBaseUpdateService" 
    android:configChanges="keyboardHidden|orientation|screenSize"/> 
+1

三江源...我通过增加屏幕尺寸 – Eric 2012-07-25 02:13:18

+0

感谢很多解决的问题,为您节省我的时间 – 2013-03-15 17:08:53

+0

感谢的人,你救了我的一天。 – 2013-05-14 11:16:06

我不是100%肯定,这将工作,但你可以尝试保存您的AsyncTask与onNonConfigurationChangedInstance()。

// Declare a member variable 
private UpdateTast task; 

public void onCreate(){ 

    // Attempt to get the asynctask instance 
    task=(UpdateTask)this.getLastNonConfigurationInstance(); 

    // If it's null create a new instance 
    if(task==null){ 
     task = new Updatetask(this.getApplicationContext()); 
     task.execute(). 

    } 

    // If it is not null, then the reference returned by getLastNonConfigurationInstance(); will be used. 
} 

// And lastly override onRetainNonConfigurationInstance method: 
@Override 
public Object onRetainNonConfigurationInstance() {   
    return task 
}