我用doInBackGround和方法的AsyncTask顯示進度如下:如何獲得上傳進度
class UploadFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
upload(ActionUrl, uploadFile, savepath, newName);
return null;
}
protected void onProgressUpdate(String... progress) {
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
的upload(ActionUrl, uploadFile, savepath, newName);
方法是如this。 而下面的代碼:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS: //we set this to 0
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
及以下稱:
private ProgressDialog mProgressDialog;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
new UploadFile().execute("test");
但進度對話框始終顯示爲0%,並且從不更新。 如何修改以更新它?
另一個問題是如何知道迄今有多少字節上傳? – brian
http://stackoverflow.com/questions/6851024/i-need-know-how-much-bytes-has-been-uploaded-for-update-a-progress-bar-android <<看看這篇文章。 – Viking