开发者

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?

开发者_StackOverflow中文版
   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;
 }

}


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;
    }
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜