Updating progress on progress dialog from AsyncTask can't build
13,012
private class DownloadFile extends AsyncTask<String, Integer, String> {
ProgressDialog mProgressDialog;
String fileName=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create ProgressBar
mProgressDialog = new ProgressDialog(Task.this);
// Set your ProgressBar Title
mProgressDialog.setTitle("Downloads");
mProgressDialog.setIcon(R.drawable.dwnload);
// Set your ProgressBar Message
mProgressDialog.setMessage("Updating App Version, Please Wait!");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// Show ProgressBar
mProgressDialog.setCancelable(false);
// mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... sUrl) {
try {
String apkurl = "http://google.com/"+fileName;
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url
.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int fileLength = c.getContentLength();
//File file = new File(PATH);
if(file.exists())
{
file.delete();
}
file.mkdirs();
File outputFile = new File(file, fileName);
final FileOutputStream fos = new FileOutputStream(outputFile);
final InputStream is = c.getInputStream();
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = is.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int) (total * 100 / fileLength));
fos.write(data, 0, count);
}
// Close connection
fos.flush();
fos.close();
is.close();
}
}
catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
mProgressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// Update the ProgressBar
mProgressDialog.setProgress(progress[0]);
}
}
Author by
darkanzali
Updated on June 04, 2022Comments
-
darkanzali almost 2 years
I can't build my project because eclipse giving error
Here is button click event who starting download
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ProgressDialog mProgressDialog; mProgressDialog = new ProgressDialog(Test.this); mProgressDialog.setMessage("Pobieranie"); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); DownloadFile downloadFile = new DownloadFile(); downloadFile.execute("http://google.com"); } });
and AsyncTask function
private class DownloadFile extends AsyncTask<String, Integer, String> { [...] @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); // Here eclipse tell that "mProgressDialog cannot be resolved" } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); mProgressDialog.setProgress(progress[0]); // Here eclipse tell that "mProgressDialog cannot be resolved" } }
So where I should insert code who create my progress dialog?