开发者

How can I pass a Context object to a thread on call

I have this code fragment:

public static class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> 
{
    private final ProgressDialog dialog = new ProgressDialog(ctx);

    protected void onPreExecute();
    protected Boolean doInBackground(final String... arg开发者_Go百科s);
    protected void onPostExecute(final Boolean success);
}

I execute this thread as

new ExportDatabaseFileTask().execute();

As you see I use a ctx as Context variable in the new ProgressDialog call, how do I pass a context to the call method?

to this one:

new ExportDatabaseFileTask().execute();*


I found the way, I had to create my own constructor, and lose the static stuff

        public ExportDatabaseFileTask(Context ctx) {
            super();
            this.ctx=ctx;
            dialog= new ProgressDialog(ctx);
        }


Just define a static setter method where you can pass the Context object


I just stumbled upon this in the Dev Guide and I believe that is exactly its purpose. android.content.ContextWrapper

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜