Java: AsyncTask subclassification

I am trying to subclass the android.os.AsyncTask class in general. I just want to add a property to it. The thing is, I still want to use it as an anonymous class.

    import android.content.Context;
    import android.os.AsyncTask;

    public class KAsyncTask extends AsyncTask<Params, Progress, Result> {
        public Context c;

    }

I tried to subclass it, but I just can't wrap it around my head, how should I do it.

Regards, EZFrag

+5
source share
3 answers

You want to say that you want it to be an abstract template class such as AsyncTask? The declaration will look like this:

public abstract class KAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
    public Context c;

}
+16
source

Thanks to Reuben, it works 100% as expected.

Here is what I have now:

    import android.content.Context;
    import android.os.AsyncTask;

    public abstract class KAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
        public Context context;

        public KAsyncTask<Params, Progress, Result> setContext(Context c){
            this.context = c;
            return this;
        }
    }

And here is how I use it:

    new KAsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Connecting to Server...", Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            //super.onPostExecute(result);
            Toast.makeText(context, "Responce Recieved.", Toast.LENGTH_LONG).show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            //Do webservice calls in here

            return null;
        }

    }.setContext(this).execute();

.

, EZFrag

+6

, AsyncTask, . AsyncTask?

AFAIK, Java . , Context Params (, , Varargs). , Params Context; String serialize/Parcel Context String. , - .

, AsyncTask? AsyncTask.

+2
source

All Articles