1. 程式人生 > >Android中執行緒的使用方法!

Android中執行緒的使用方法!

public class ListProgressDemo extends ListActivity {  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.listprogress);  ((Button) findViewById(R.id.load_Handler)).setOnClickListener(new View.OnClickListener(){  @Override  public void onClick(View view) {  data = null;  data = new ArrayList();  adapter = null;  showDialog(PROGRESS_DIALOG);  new ProgressThread(handler, data).start();  }  });  }  @Override  protected Dialog onCreateDialog(int id) {  switch(id) {  case PROGRESS_DIALOG:  return ProgressDialog.show(this, "",  "Loading. Please wait...", true);  default: return null;  }  }  private class ProgressThread extends Thread {  private Handler handler;  private ArrayList data;  public ProgressThread(Handler handler, ArrayList data) {  this.handler = handler;  this.data = data;  }  @Override  public void run() {  for (int i=0; i<8; i++) {  data.add("ListItem"); //後臺資料處理  try {  Thread.sleep(100);  }catch(InterruptedException e) {  Message msg = handler.obtainMessage();  Bundle b = new Bundle();  b.putInt("state", STATE_ERROR);  msg.setData(b);  handler.sendMessage(msg);  }  }  Message msg = handler.obtainMessage();  Bundle b = new Bundle();  b.putInt("state", STATE_FINISH);  msg.setData(b);  handler.sendMessage(msg);  }  }  // 此處甚至可以不需要設定Looper,因為Handler預設就使用當前執行緒的Looperprivate final Handler handler = new Handler(Looper.getMainLooper()) {  public void handleMessage(Message msg) { // 處理Message,更新ListView  int state = msg.getData().getInt("state");  switch(state){  case STATE_FINISH:  dismissDialog(PROGRESS_DIALOG);  Toast.makeText(getApplicationContext(),  "載入完成!",  Toast.LENGTH_LONG)  .show();  adapter = new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1,  data );  setListAdapter(adapter);  break;  case STATE_ERROR:  dismissDialog(PROGRESS_DIALOG);  Toast.makeText(getApplicationContext(),  "處理過程發生錯誤!",  Toast.LENGTH_LONG)  .show();  adapter = new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1,  data );  setListAdapter(adapter);  break;  default:  }  }  };  private ArrayAdapter adapter;  private ArrayList data;  private static final int PROGRESS_DIALOG = 1;  private static final int STATE_FINISH = 1;  private static final int STATE_ERROR = -1;  }  這個例子,我自己寫完後覺得還是有點亂,要稍微整理才能看明白執行緒間互動的過程以及資料的前後變化。隨後瞭解到AsyncTask類,相應修改後就很容易明白了!  2.3 AsyncTask  AsyncTask版:  ((Button) findViewById(R.id.load_AsyncTask)).setOnClickListener(new View.OnClickListener(){  @Override  public void onClick(View view) {  data = null;  data = new ArrayList();  adapter = null;  //顯示ProgressDialog放到AsyncTask.onPreExecute()裡  //showDialog(PROGRESS_DIALOG);  new ProgressTask().execute(data);  }  });private class ProgressTask extends AsyncTask, Void, Integer> {  /* 該方法將在執行實際的後臺操作前被UI thread呼叫。可以在該方法中做一些準備工作,如在介面上顯示一個進度條。*/  @Override  protected void onPreExecute() {  // 先顯示ProgressDialog  showDialog(PROGRESS_DIALOG);  }  /* 執行那些很耗時的後臺計算工作。可以呼叫publishProgress方法來更新實時的任務進度。 */  @Override  protected Integer doInBackground(ArrayList... datas) {  ArrayList data = datas[0];  for (int i=0; i<8; i++) {  data.add("ListItem");  }  return STATE_FINISH;  }  /* 在doInBackground 執行完成後,onPostExecute 方法將被UI thread呼叫,  * 後臺的計算結果將通過該方法傳遞到UI thread.  */  @Override  protected void onPostExecute(Integer result) {  int state = result.intValue();  switch(state){  case STATE_FINISH:  dismissDialog(PROGRESS_DIALOG);  Toast.makeText(getApplicationContext(),  "載入完成!",  Toast.LENGTH_LONG)  .show();  adapter = new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1,  data );  setListAdapter(adapter);  break;  case STATE_ERROR:  dismissDialog(PROGRESS_DIALOG);  Toast.makeText(getApplicationContext(),  "處理過程發生錯誤!",  Toast.LENGTH_LONG)  .show();  adapter = new ArrayAdapter(getApplicationContext(),  android.R.layout.simple_list_item_1,  data );  setListAdapter(adapter);  break;  default:  }  }