Android AsyncTask的使用

來源:互聯網
上載者:User

整理自:http://blog.sina.com.cn/s/blog_9ec5eef701019s3t.html 與http://www.apkbus.com/android-59747-1-1.html

簡介

AsyncTask可以使得使用UI線程變的更容易更適當,它可以在後台運行一些操作然後在UI上展現,不用操作具體的線程和handlers
一個 asynchronous task包括三種基本類型(調用參數,進度和結果),和四個步驟(調用開始,在後台運行,處理進度,結束)
), and most often will override a second one (onPostExecute(Result).)

使用方法描述
Asynchronous Task必須是作為一個子類來使用,
task執行個體必須在UI線程建立
execute(Params...)必須在UI線程調用
不要手工調用onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...)。
task只可以execute一次,執行多次就報異常

                                           
                                       <Integer,Integer,String>
public class FirstAsyncTask extendsAsyncTask<Void,Void,Void>{
//第一個參數是指doInbackground接受的參數類型
//第二個參數定義onprogressupdate的參數
//第三個參數定義doinbackground傳回值類型和onpostexecute的參數類型
@Override
protected VoiddoInBackground(Void...arg0)//在非同步線程中,不能更新UI,三個點說明參數是變長的,有可能是一個整形,兩個整形,arg0是傳入的數組。
{
publishprogress(i);//調用onprogressupdate函數進行UI的更新;
}

protected voidonPreExecute(){}//在執行doinbackground方法前調用。在UI線程中,可以更新UI。

protected void onPostExecute(Stringresult)//在執行doinbackground方法後調用。在UI線程中,可以更新UI。

protected void publishProgress(){}

protected void onProgressUpdate(Integer ... arg1){}

}

一個例子
類的定義
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
}
類的使用
new DownloadFilesTask().execute(url1, url2, url3);

三種基本類型的說明
Params, 傳給task的參數的類型
Progress, 表示進度單位的類型
Result, 傳回型別
不是所有的task都需要定義類型,如果沒有則使用void,如下所示
private class MyTask extends AsyncTask<Void, Void, Void> { ... }

四個步驟的說明
onPreExecute():
在task被執行之後,立刻調用

doInBackground(Params...):
onPreExecute執行完畢後,執行該方法,參數傳到了這個方法中,執行完畢後必須返回一個值,還可以使用 publishProgress(Progress...) 發布進度到onProgressUpdate(Progress...),便於更新進度

onProgressUpdate(Progress...):
publishProgress(Progress...)被調用後,就執行該方法,顯示進度資訊
通常是顯示一個進度條,或在text域裡顯示日誌資訊

onPostExecute(Result)
當doInBackground(Params...)執行完畢後即執行該方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.