Loading progress in AndyncTask android

I am trying to create a download process. I have a class that extends AsyncTask: Public class DownloadFileTask extends AsyncTask

When the download starts, I want to create a progress:

  @Override
  protected void onPreExecute() {



   progressDialog = new ProgressDialog(whatContext);
   progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   progressDialog.setMessage("Loading...");
   progressDialog.setCancelable(false);
   progressDialog.show();
  }

      

But I don't know what context I should give the new ProgressDialog because I am not in the Activity class. I tried to give some context, but there was an error:

There is no instance of type Main_Tab available in Scope

So how could I create this progress?

Also I wanted to create progress not in this class (because I want separate functions and design), but I didn't figure out how to do it.

Thanks guys for the help.

+3


source to share


3 answers


 public class DownloadFileTask extends AsyncTask{
Context mContext;

    public DownloadFileTask(Context context) {
            this.mContext = context;

        }


     @Override
      protected void onPreExecute() {



       progressDialog = new ProgressDialog(mContext);
       progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
       progressDialog.setMessage("Loading...");
       progressDialog.setCancelable(false);
       progressDialog.show();
      }
    }

      

To start a DownloadTask call like



 DownloadFileTask task = new DownloadFileTask(MyActivity.This);
 task.execute();

      

+3


source


You have to create a constructor for AsyncTask

that takes an object Context

as a parameter, for example:

public DownloadFileTask(Context context) {
    this.context = context;
}

      



Then you can use the field Context

to initialize ProgressDialog

. On the second question, there is not enough information to answer it. Hope this helps.

+2


source


My guess is that passing context to your custom Loadins AsyncTask constructor might do the trick.

0


source







All Articles