2012-07-05 56 views
1

我正在從Dropbox下載一個需要幾秒鐘的文件。我想添加一個ProgressDialog下載,但我不知道該怎麼做。如何添加ProgressDialog

public class DownloadFile extends AsyncTask<Void, Long, Boolean> { 
    DownloadFile(Context context ,DropboxAPI<?> mApi ,String dropboxpath,String sdpath,int pos,int s,ArrayList<String> folder) throws DropboxException { 
     FileOutputStream mFos; 
     File file=new File(sdpath); 
     String path = dropboxpath; 
     try{ 
      mFos = new FileOutputStream(file); 
      mApi.getFile(path, null, mFos, null); 
     }catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 

    @Override 
    protected Boolean doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

回答

3

這樣來做:

public final class DownloadFile extends AsyncTask<Void, Long, Boolean> { 

private Context context; 
private ProgressDialog progressDialog; 

public DownloadFile (Context context) { 
    this.context = context; 
} 

/* 
* @see android.os.AsyncTask#onPreExecute() 
*/ 
@Override 
protected void onPreExecute() { 
    try { 
     progressDialog = ProgressDialog.show(context, "", "message", true); 
    } catch (final Throwable th) { 
     //TODO 
    } 
} 

/* 
* @see android.os.AsyncTask#doInBackground(Params[]) 
*/ 
@Override 
protected Boolean doInBackground(Void... arg0) { 
    //do something 
} 

    @Override 
protected void onProgressUpdate(String... progress) { 
    //do something 
    super.onProgressUpdate(progress); 
} 

/* 
* @see android.os.AsyncTask#onPostExecute(java.lang.Object) 
*/ 
@Override 
protected void onPostExecute(Boolean result) { 
    progressDialog.dismiss(); 
} } 
0

見居然還有的AsyncTask的4種方法:

  1. onPreExecute() - 你可以在這裏做一些前執行任務。
  2. doInBackground() - 您可以在這裏執行一些後臺工作。
  3. onPostExecute() - 您可以在這裏執行執行後任務。意味着像在ListView中顯示數據,更新TextView等。
  4. onProgressUpdate() - 在後臺操作正在進行時更新UI。

所以你的情況,你可以顯示進度對話框或進度條裏面的AsyncTask的onPreExecute()方法和罷免(()相同的內部onPostExecute()

0

使用此簡單代碼@sachin

public class DownloadFile extends AsyncTask<Void, Void, Void> { 

    Home home; 
    ProgressDialog dialog = null; 

    public DownloadFile(Home home) { 
     // TODO Auto-generated constructor stub 
     this.home = home; 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     //Call hare method for download 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     dialog.dismiss(); 

    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     dialog = ProgressDialog.show(home, "Downloading......", "", true); 
    } 

} 
0

本文可爲您提供幫助:

http://huuah.com/android-progress-bar-and-thread-updating/

如果你線程的run()方法裏,你可以調用這樣的功能:

public boolean download(String url, String path, String fileName, Handler progressHandler) { 
    try { 
     URL sourceUrl = new URL(formatUrl(url)); 
     if (fileName == null || fileName.length() <= 0) { 
      fileName = sourceUrl.getFile(); 
     } 
     if (fileName == null || fileName.length() <= 0) { 
      throw new Exception("EMPTY_FILENAME_NOT_ALLOWED"); 
     } 
     File targetPath = new File(path); 
     targetPath.mkdirs(); 
     if (!targetPath.exists()) { 
      throw new Exception("MISSING_TARGET_PATH"); 
     } 
     File file = new File(targetPath, fileName); 
     URLConnection ucon = sourceUrl.openConnection(); 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     ByteArrayBuffer baf = new ByteArrayBuffer(100); 
     int current = 0; 
     int totalSize = ucon.getContentLength(); 
     while ((current = bis.read()) != -1) { 
      baf.append((byte) current); 
      // BEGIN - Handler feedback 
      if (progressHandler != null && (baf.length() % 100) == 0) { 
       Message msg = progressHandler.obtainMessage(); 
       Bundle b = new Bundle(); 
       if (totalSize > 0) { 
        b.putInt("total", totalSize); 
        b.putInt("step", baf.length()); 
        b.putBoolean("working", true); 
       } 
       msg.setData(b); 
       progressHandler.handleMessage(msg); 
      } 
      // END 
     } 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(baf.toByteArray()); 
     fos.close(); 
     // BEGIN - Handler feedback 
     if (progressHandler != null) { 
      Message msg = progressHandler.obtainMessage(); 
      Bundle b = new Bundle(); 
      if (totalSize > 0) { 
       b.putInt("total", 0); 
       b.putInt("step", 0); 
       b.putBoolean("working", false); 
      } 
      msg.setData(b); 
      progressHandler.handleMessage(msg); 
     } 
     // END 
     return file.exists(); 
    } 

做這樣一來,您對您下載真正的進步更準確的反饋(每字節字節)。