2015-05-14 55 views

回答

3

AsyncTasks是要走的路。

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[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

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

    protected void onPostExecute(Long result) { 
     showDialog("Downloaded " + result + " bytes"); 
    } 
} 
+0

如果我想要在對話框中顯示加載圖標而不是使用publishProgress顯示遞增功能,該怎麼辦?我試過爲此使用AsyncTask,但只得到一個Handler來工作 –

+2

@ Sorrow123444在onPreExecute中顯示ProgressDialog,然後在onPostExecute中關閉。 – dominicoder