[Android]異步任務AsyncTask使用解析

AsyncTask主要用來更新UI線程,比較耗時的操作可以在AsyncTask中使用。

AsyncTask是個抽象類,使用時需要繼承這個類,然後調用execute()方法。注意繼承時需要設定三個泛型Params,Progress和Result的類型,如AsyncTask<Void,Inetger,Void>:

  • Params是指調用execute()方法時傳入的參數類型和doInBackgound()的參數類型
  • Progress是指更新進度時傳遞的參數類型,即publishProgress()和onProgressUpdate()的參數類型
  • Result是指doInBackground()的返回值類型
上面的說明涉及到幾個方法:
  • doInBackgound() 這個方法是繼承AsyncTask必須要實現的,運行於後臺,耗時的操作可以在這裏做
  • publishProgress() 更新進度,給onProgressUpdate()傳遞進度參數
  • onProgressUpdate() 在publishProgress()調用完被調用,更新進度
好了,看下實際的例子,瞭解一下怎麼使用吧:
[java] view plaincopy
  1. public class MyActivity extends Activity  
  2. {  
  3.     private Button btn;  
  4.     private TextView tv;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         btn = (Button) findViewById(R.id.start_btn);  
  11.         tv = (TextView) findViewById(R.id.content);  
  12.         btn.setOnClickListener(new Button.OnClickListener(){  
  13.             public void onClick(View v) {  
  14.                 update();  
  15.             }  
  16.         });  
  17.     }  
  18.     private void update(){  
  19.         UpdateTextTask updateTextTask = new UpdateTextTask(this);  
  20.         updateTextTask.execute();  
  21.     }  
  22.   
  23.     class UpdateTextTask extends AsyncTask<Void,Integer,Integer>{  
  24.         private Context context;  
  25.         UpdateTextTask(Context context) {  
  26.             this.context = context;  
  27.         }  
  28.   
  29.         /** 
  30.          * 運行在UI線程中,在調用doInBackground()之前執行 
  31.          */  
  32.         @Override  
  33.         protected void onPreExecute() {  
  34.             Toast.makeText(context,"開始執行",Toast.LENGTH_SHORT).show();  
  35.         }  
  36.         /** 
  37.          * 後臺運行的方法,可以運行非UI線程,可以執行耗時的方法 
  38.          */  
  39.         @Override  
  40.         protected Integer doInBackground(Void... params) {  
  41.             int i=0;  
  42.             while(i<10){  
  43.                 i++;  
  44.                 publishProgress(i);  
  45.                 try {  
  46.                     Thread.sleep(1000);  
  47.                 } catch (InterruptedException e) {  
  48.                 }  
  49.             }  
  50.             return null;  
  51.         }  
  52.   
  53.         /** 
  54.          * 運行在ui線程中,在doInBackground()執行完畢後執行 
  55.          */  
  56.         @Override  
  57.         protected void onPostExecute(Integer integer) {  
  58.             Toast.makeText(context,"執行完畢",Toast.LENGTH_SHORT).show();  
  59.         }  
  60.   
  61.         /** 
  62.          * 在publishProgress()被調用以後執行,publishProgress()用於更新進度 
  63.          */  
  64.         @Override  
  65.         protected void onProgressUpdate(Integer... values) {  
  66.             tv.setText(""+values[0]);  
  67.         }  
  68.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章