Mascarpone
Mascarpone

Reputation: 2556

Retrieve context in an abstract, non activity class

I'm trying to build my abstract implementation of AsyncTask and I would like to insert a custom ProgressDialog. How can I get the context outside of an Activity Class?

   abstract public class DataPoller extends AsyncTask<Void, Void, Void> {

 Context mContext = getApplicationContext();

 ProgressDialog dialog = new ProgressDialog(mContext);

 @Override
 protected void onPreExecute() {



  dialog.setMessage("Polling data...");
  dialog.show();


 }


 @Override
 protected void onPostExecute(Void unused) {

  if ( dialog.isShowing() ) {

   dialog.dismiss();

  }


 }

 @Override
 protected Void doInBackground(Void... params) {

  int tmp=0;

  for (int ii = 0; ii<1000; ii ++) {

   for (int jj = 0; jj<1000; jj ++) {

    tmp = ( tmp + 3 ) % 167;     

   }

  }
  return null;
 }

}

Upvotes: 2

Views: 1725

Answers (1)

dave.c
dave.c

Reputation: 10918

You could pass it into the constructor:

abstract public class DataPoller extends AsyncTask<Void, Void, Void> {
    ...
    Context mContext;
    ...
    DataPoller(Context context){
        super();
        this.mContext = context;
    }
    ...
}

Upvotes: 4

Related Questions