Android中通過AsyncTask類來製作炫酷進度條的執行個體教程_Android

來源:互聯網
上載者:User

AsyncTask (API level 3,所以幾乎所有目前在市面上流通的 Android 版本皆可使用)
是除 Thread 外的另一種選擇,Android 團隊鼓勵主執行緒(UI thread) 專註於操作 & 畫面的流暢呈現,
其餘工作 (如網路資料傳輸、檔案/磁碟/資料存取) 最好都在背景執行;
Thread 通常要搭配 Handler 使用,而 AsyncTask 用意在簡化背景執行 thread 程式碼的撰寫。

如果您預期要執行的工作能在幾秒內完成,就可以選擇使用 AsyncTask,若執行的時間很長,
Android 則強烈建議採用 Executor, ThreadPoolExecutor and FutureTask。

要使用 AsyncTask,必定要建立一個繼承自 AsyncTask 的子類別,並傳入 3 項資料:

  • Params -- 要執行 doInBackground() 時傳入的參數,數量可以不止一個
  • Progress -- doInBackground() 執行過程中回傳給 UI thread 的資料,數量可以不止一個
  • Rsesult -- 傳回執行結果

若您沒有參數要傳入,則填入 Void (注意 V 為大寫)。

AsyncTask 的運作有 4 個階段:

  • onPreExecute -- AsyncTask 執行前的準備工作,例如畫面上顯示進度表,
  • doInBackground -- 實際要執行的程式碼就是寫在這裡,
  • onProgressUpdate -- 用來顯示目前的進度,
  • onPostExecute -- 執行完的結果 - Result 會傳入這裡。

除了 doInBackground,其他 3 個 method 都是在 UI thread 呼叫


炫酷進度條執行個體
我們以一個執行個體來說明,“點擊按鈕開始下載QQAndroid安裝包,然後顯示一個對話方塊來反饋下載進度”。我們先初始化一個對話方塊,由於要顯示進度,我們用Github上面一個能夠顯示百分比的進度條 NumberProgressbar,啟動任務的按鈕我們使用 circlebutton,一個有酷炫動畫的按鈕,Github上面有很多非常好的開源項目,當然炫酷的控制項是其中一部分了,後面有機會,會去學習一些比較流行的控制項它們的實現原理,今天就暫且拿來主義了~~。

1.先初始化進度條提示對話方塊。

 builder = new AlertDialog.Builder(     MainActivity.this); LayoutInflater inflater = LayoutInflater.from(MainActivity.this); mDialogView = inflater.inflate(R.layout.progress_dialog_layout, null); mNumberProgressBar = (NumberProgressBar)mDialogView.findViewById(R.id.number_progress_bar); builder.setView(mDialogView); mDialog = builder.create();

2.設定按鈕點擊事件。

 findViewById(R.id.circle_btn).setOnClickListener(new View.OnClickListener(){   @Override   public void onClick(View v) {     dismissDialog();     mNumberProgressBar.setProgress(0);     myTask = new MyAsyncTask();     myTask.execute(qqDownloadUrl);   } });

3.DownloadAsyncTask實現,有點長。

private class DownloadAsyncTask extends AsyncTask<String , Integer, String> { @Override protected void onPreExecute() {   super.onPreExecute();   mDialog.show(); } @Override protected void onPostExecute(String aVoid) {   super.onPostExecute(aVoid);   dismissDialog(); } @Override protected void onProgressUpdate(Integer... values) {   super.onProgressUpdate(values);   mNumberProgressBar.setProgress(values[0]); } @Override protected void onCancelled(String aVoid) {   super.onCancelled(aVoid);   dismissDialog(); } @Override protected void onCancelled() {   super.onCancelled();   dismissDialog(); } @Override protected String doInBackground(String... params) {   String urlStr = params[0];   FileOutputStream output = null;   try {     URL url = new URL(urlStr);     HttpURLConnection connection = (HttpURLConnection)url.openConnection();     String qqApkFile = "qqApkFile";     File file = new File(Environment.getExternalStorageDirectory() + "/" + qqApkFile);     if (file.exists()) {       file.delete();     }     file.createNewFile();     InputStream input = connection.getInputStream();     output = new FileOutputStream(file);     int total = connection.getContentLength();     if (total <= 0) {       return null;     }     int plus = 0;     int totalRead = 0;     byte[] buffer = new byte[4*1024];     while((plus = input.read(buffer)) != -1){       output.write(buffer);       totalRead += plus;       publishProgress(totalRead * 100 / total);       if (isCancelled()) {         break;       }     }     output.flush();   } catch (MalformedURLException e) {     e.printStackTrace();     if (output != null) {       try {         output.close();       } catch (IOException e2) {         e2.printStackTrace();       }     }   } catch (IOException e) {     e.printStackTrace();     if (output != null) {       try {         output.close();       } catch (IOException e2) {         e2.printStackTrace();       }     }   } finally {     if (output != null) {       try {         output.close();       } catch (IOException e) {         e.printStackTrace();       }     }   }   return null; }}

這樣一個簡單的下載檔案檔案就基本實現了,到目前為止談不上技巧,但是現在我們有一個問題,就是如果我們的Activity正在後台執行一個任務,可能耗時較長,那使用者可能會點擊返回退出Activity或者退出App,那麼背景工作不會立即退出,如果AsyncTask內部有Activity中成員變數的引用,還會造成Activity的回收延時,造成一段時間內的記憶體泄露,所以我們需要加上下面的第四步處理。

4.onPause中判斷應用是否要退出,從而決定是否取消AsyncTask執行。

@Overrideprotected void onPause() { super.onPause(); if (myTask != null && isFinishing()) {   myTask.cancel(false); }}

這樣我們的非同步任務就會在Activity退出時,也隨之取消任務執行,順利被系統銷毀回收,第四步很多時候會被遺漏,而且一般也不會有什麼致命的問題,但是一旦出問題了,就很難排查,所以遵循編碼規範還是有必要的。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.