我试图实现以下代码来处理屏幕方向更改.
****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 – 防止活动重新创建.
解决方案2 – 将AsyncTask放入片段.
解决方案3 – 锁定屏幕方向
解决方案4 – 防止活动重新创建.
参考:http://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/
…..
发生问题的原因是当配置更改时重新创建活动,如方向更改等.
您可以锁定asyntask的onPreExecuted()方法中的方向更改,并在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(); } }