我使用一個公共的AsynTask來下載數據,我試圖顯示一個進度條,它會顯示下載進度。我認爲我的代碼是正確的,但我得到的是一個微調進度對話框。我錯過了什麼嗎?爲什麼不顯示進度條?這是代碼。 謝謝你的指點。我想要一個進度條,但得到一個微調progressdialog
public class FileDownloader extends AsyncTask<String, Integer, Void>
{
private Context _appContext;
private HttpURLConnection _urlConn;
private ProgressDialog _progressDia = null;
private DialogInterface.OnCancelListener _progDiaCancelListener = new DialogInterface.OnCancelListener()
{
/**
* When the progress dialog is canceled, stop the GET request.
*/
public void onCancel(DialogInterface dialog)
{
FileDownloader.this.cancel(true);
}
};
/**
* Constructor.
* @param appContext
*/
public FileDownloader(Context appContext)
{
_appContext = appContext;
_progressDia = new ProgressDialog(_appContext, ProgressDialog.STYLE_HORIZONTAL);
_progressDia.setMax(100);
_progressDia.setTitle(_appContext.getString(R.string.diaHeader1));
_progressDia.setMessage(_appContext.getString(R.string.diaBody1));
_progressDia.setCancelable(true);
_progressDia.setIndeterminate(false);
_progressDia.setOnCancelListener(_progDiaCancelListener);
}
// Runs on the UI thread
@Override
protected void onPreExecute()
{
_progressDia.setProgress(0);
_progressDia.show();
}
@Override
protected Void doInBackground(String... args)
{
String dloadURL = args[0],
saveLoc = args[1];
...
...
while((len = input.read(buf)) > 0)
{
output.write(buf, 0, len);
total += len;
publishProgress((int)total * 100/lenghtOfFile);
}
...
...
}
catch(SocketTimeoutException ex)
{
}
finally
{
...
}
// This is executed on main UI thread.
@Override
protected void onProgressUpdate(Integer... values)
{
_progressDia.setProgress(values[0]);
}
@Override
protected void onCancelled()
{
...
}
// This is executed on main UI thread.
@Override
protected void onPostExecute(Void result)
{
removeProgressDialog();
...
}
/**
* Remove the message dialog, if still showing.
*/
private void removeProgressDialog()
{
if(_progressDia != null && _progressDia.isShowing())
_progressDia.dismiss();
}
}
感謝 - 這工作。只有我必須改變的是使用單個參數構造函數並設置進度風格。 – 2010-07-25 04:52:12
謝謝,也幫助了我。我的錯誤是我正在調用'ProgressDialog.show(...);'然後設置所有其他參數 – Glogo 2015-03-10 10:07:25