In my application I'm downloading an image file. I want to show the download speed on a progress bar for the download.
How is it possible through AsyncTask
Concept?
In my application I'm downloading an image file. I want to show the download speed on a progress bar for the download.
How is it possible through AsyncTask
Concept?
See the code snippet
public static class MyDownloadTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// receive the published update here
// progressBar.setProgress(values[0]);
}
@Override
protected Void doInBackground(Void... params) {
// publish your download progress here
// publishProgress(10);
return null;
}
}
With this example you can set download speed on your progressdialog
public class AsyncDownload extends AsyncTask<Void, Double, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Speed: " + 0.0);
progressDialog.show();
}
@Override
protected String doInBackground(Void... voids) {
// AsyncDownload
Double speed = 0.0;
// Calculate speed
publishProgress(speed);
return null;
}
@Override
protected void onProgressUpdate(Double... values) {
super.onProgressUpdate(values);
progressDialog.setMessage("Speed " + values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
to calculate download speed you can use this example Measuring Download Speed Java