java - Trouble using AsyncTask outside of Activity -


i've created asynctask inner class of activity. , works good.

but now, since want reuse asynctask inside many other activity, i'm trying use separate class asynctask.

the problem lost context reference. so, example code:

    @override     protected void onpreexecute() {         pdialog = progressdialog.show(myactvity.this, "wait...", "sending data ...", true);         pdialog.setcancelable(false);         super.onpreexecute();     } 

can't work, since context in show method cannot resolved.

how can solve problem?

create asynctask class in following way :-

public class mytask extends asynctask<void,void,void>{    private context mcontext; // context reference    private progressdialog pdialog;    public mytask(context context){ //constructor     this.mcontext = context;   }   @override     protected void onpreexecute() {         pdialog = progressdialog.show(mcontext, "wait...", "sending data ...", true);         pdialog.setcancelable(false);         super.onpreexecute();     } } 

and use :-

mytask mtask = new mytask(activity.this); mtask.execute(); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -