2012-07-24 35 views
9

我試圖實現以下代碼來處理屏幕方向更改。如何使用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.

你有什麼想法是什麼問題?

回答

2

解決方案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

27

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

0

我不是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 
}