How To Add Progress Bar To Standalone Asynctask?
Solution 1:
classMyTaskextendsAsyncTask<Request, Void, Result> {
protectedProgressDialog progressDialog;
@OverrideprotectedvoidonPreExecute()
{
super.onPreExecute();
progressDialog = ProgressDialog.show(YourActivity.this, "", "", true, false);
}
@OverrideprotectedBooleandoInBackground(Request... params) {
// do some work here returntrue;
}
@OverrideprotectedvoidonPostExecute(Result res) {
progressDialog.dismiss();
}
}
Solution 2:
Add a ProgressBar(this is what it's actually called, Spinners are like drop down menus in Android) to the layout of the Activity where you're initializing your AsyncTask.
Then make two functions startProgress()
and stopProgress()
, which start and stop the progress bar.
Give your AsyncTask a reference to the Activity, either by sending it during initialization or execution, or making a function in your asyncTask setActivity(MyActivity activity)
and call it between your AsyncTask initialization and execution.
Override the onPreExecute()
of your AsyncTask to call activity.startProgress()
and onPostExecute()
to call activity.stopProgress()
.
EDIT: You can also try passing a reference to the ProgressBar in the constructor of your AsyncTask. Get the reference to the ProgressBar in the onCreate()
method of your activity, then add it in the AsyncTask constructor. In the onPreExecute()
and onPostExecute()
methods of the AsyncTask, start and stop the progress bars accordingly.
Solution 3:
You can pass various parameters to an AsyncTask
, not just one!
One way to do this is to make member variables in your AsyncTask and initialize them using a constructor that accepts parameters. For example:
privateclassMyAsyncTaskextendsAsyncTask<null, null, null> {
StringmFirstParam=null;
ContextmContext=null;
// Constructor which accepts parameterspublicMyAsyncTask(String _firstParam, Context _context){
this.mFirstParam = _firstParam;
this.mContext = _context;
}
}
When you create an instance of your AsyncTask
, create it as follows:
MyAsyncTasktask=newMyAsyncTask(myFirstStringParam, mySecondContextParam);
task.execute();
Now you can use both these parameters throughout the scope of your AsyncTask
.
Consider passing the ImageView
containing of "loading" image and set its Visibility to View.GONE
once you have finished downloading your data.
i.e. download your data in the doInBackground
method and then change the ImageViews visibility to View.GONE
in the onPostExecute
method
Solution 4:
I think you can pass the progress bar instance to AsyncTask when your create it in its constructor. Here is a downloader example by using AsyncTask -
publicvoiddownloadFile(String url, String path, ProgressDialog progress) {
DownloadFileAsyncdownloader=newDownloadFileAsync(progress);
Filefile=newFile(path);
if (file.exists()) {
file.delete();
}
downloader.execute(url, path);
}
classDownloadFileAsyncextendsAsyncTask<String, String, String> {
privatefinal WeakReference<ProgressDialog> progressbarReference;
publicDownloadFileAsync(ProgressDialog progress) {
progressbarReference = newWeakReference<ProgressDialog>(progress);
}
@OverrideprotectedvoidonPreExecute() {
super.onPreExecute();
}
@Overrideprotected String doInBackground(String... aurl) {
int count;
try {
URLurl=newURL(aurl[0]);
URLConnectionconexion= url.openConnection();
conexion.connect();
intlenghtOfFile= conexion.getContentLength();
/*
* android.util.Log.v("downloadFile", "Lenght of file: " +
* lenghtOfFile + ":" + aurl[1]);
*/InputStreaminput=newBufferedInputStream(url.openStream());
OutputStreamoutput=newFileOutputStream(aurl[1]);
try {
byte data[] = newbyte[1024];
longtotal=0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""
+ (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
} finally {
if (output != null) {
output.flush();
output.close();
}
if (input != null) {
input.close();
}
}
} catch (Exception e) {
ProgressDialogp=null;
if (progressbarReference != null) {
p = progressbarReference.get();
}
if (p != null && p.isShowing()) {
p.dismiss();
}
}
returnnull;
}
protectedvoidonProgressUpdate(String... progress) {
if (progressbarReference != null) {
ProgressDialogp= progressbarReference.get();
if (p != null) {
p.setProgress(Integer.parseInt(progress[0]));
}
}
}
@OverrideprotectedvoidonPostExecute(String unused) {
ProgressDialogp=null;
if (progressbarReference != null) {
p = progressbarReference.get();
}
if (p != null && p.isShowing()) {
p.dismiss();
}
}
}
ProgressDialog is a custom Dialog which have a progress bar inside it. Hope it helps.
Post a Comment for "How To Add Progress Bar To Standalone Asynctask?"