Multiple input parameters for the method execute() of AsyncTask

前端 未结 2 1722
野的像风
野的像风 2021-02-07 21:09

everyone. I have read on the android site the description of the excute() of AsyncTask:

public final AsyncTask execute (Params... p

相关标签:
2条回答
  • 2021-02-07 21:47

    Read Params... params as Params[] params. You can send as many params as you want.

    0 讨论(0)
  • 2021-02-07 21:59

    Here is how I got it to pass multiple parameters. You could do it as Boris described, but what if you pass different types?

    First, create your AsyncTask as normal, but within it, create a constructor:

        private class StartTask extends AsyncTask<Context, Void, Boolean> 
        {
            private ProgressDialog progress;
            private String strAction="";
    
            public StartTask(ProgressDialog progress, String Action)
            {
                this.progress = progress;
                this.strAction = Action;
            }
        }
    

    Now, on your event or anything else, when you want to kick off the action you call your AsyncTask and pass as many parameters as you want.

        ProgressDialog progress = new ProgressDialog(this);
        progress.setMessage("Loading...");
        String strAction = "this_is_a_string";
        new StartTask(progress, strAction).execute(this);
    

    You can see that calling "StartTask" and passing the constuctor parameters will now assign the variables within the StartTask class.

    0 讨论(0)
提交回复
热议问题