AsyncTask
AsyncTask「異步任務」,在Android4.0之後是不能在主執行緒進行網路行為的,避免影響使用者體驗,必須在背景執行,例如:文件上傳,資料下載,檔案存取等等。
要處理背景執行有以下幾種可以選擇:
Service,Executor,Thread,ThreadPoolExecutor,FutureTask,AsyncTask......
若需要長時間執行,Android官方建議使用Executor,ThreadPoolExecutor和FutureTask。若只需要幾秒鐘即可完成,就可以選擇使用AsyncTask!
要使用AsyncTask,就必須先建立一個繼承AsyncTask的類別
其中包含3個參數和4個階段,分別如下列所示。
Params:
執行時傳入的參數。
(The Type of the parameters sent to the task upon execution.)
Progerss:
執行過程中的進展。
(The type of the progress units published during the background computation.)
Result:
要回傳的執行結果。
(The type of the result of the background computation.)
4個步驟
onPreExecute():
執行前的準備工作,例如:顯示進度條。
(Invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.)
doInBackground(Params...):
要在背景執行的任務就寫在這裡,切記在此無法跟UI互動,僅能處理資料或是計算,執行結果必須等到onPostExecute才能顯示在UI介面上。
(Invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step.)
onProgressUpdate(Progress...):
主要用來顯示進度,更新進度條上的值。
(Invoked on the UI thread after a call to publishProgress(Progress...). The timing of the exection is defined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.)
onPostExecute(Result):
doInBackground執行完後會執行此方法,主要用來回傳結果給UI顯示。
(Invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.)
以下是簡單的程式碼範例,同時實作了進度條。

繼承AsyncTask的類別,並覆寫
onPreExecute和doInBackground方法。

覆寫onProgressUpdate和onPostExecute方法。

將程式碼跑起來就如同下圖所示,任務在背景執行時,畫面上會顯示進度。

AsyncTask架構大致上就長這樣,淺顯易懂,程式碼也容易整理。
參考網站:
Developer Console - AsyncTask
老灰鴨的筆記本
要處理背景執行有以下幾種可以選擇:
Service,Executor,Thread,ThreadPoolExecutor,FutureTask,AsyncTask......
若需要長時間執行,Android官方建議使用Executor,ThreadPoolExecutor和FutureTask。若只需要幾秒鐘即可完成,就可以選擇使用AsyncTask!
要使用AsyncTask,就必須先建立一個繼承AsyncTask的類別
其中包含3個參數和4個階段,分別如下列所示。
Params:
執行時傳入的參數。
(The Type of the parameters sent to the task upon execution.)
Progerss:
執行過程中的進展。
(The type of the progress units published during the background computation.)
Result:
要回傳的執行結果。
(The type of the result of the background computation.)
4個步驟
onPreExecute():
執行前的準備工作,例如:顯示進度條。
(Invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.)
doInBackground(Params...):
要在背景執行的任務就寫在這裡,切記在此無法跟UI互動,僅能處理資料或是計算,執行結果必須等到onPostExecute才能顯示在UI介面上。
(Invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step.)
onProgressUpdate(Progress...):
主要用來顯示進度,更新進度條上的值。
(Invoked on the UI thread after a call to publishProgress(Progress...). The timing of the exection is defined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.)
onPostExecute(Result):
doInBackground執行完後會執行此方法,主要用來回傳結果給UI顯示。
(Invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.)
以下是簡單的程式碼範例,同時實作了進度條。

繼承AsyncTask的類別,並覆寫
onPreExecute和doInBackground方法。

覆寫onProgressUpdate和onPostExecute方法。

將程式碼跑起來就如同下圖所示,任務在背景執行時,畫面上會顯示進度。

AsyncTask架構大致上就長這樣,淺顯易懂,程式碼也容易整理。
參考網站:
Developer Console - AsyncTask
老灰鴨的筆記本
留言
張貼留言