Passing parameters to Asynctask

后端 未结 4 1424
日久生厌
日久生厌 2021-01-31 10:48

I am using Async tasks to get string from the menu activity and load up some stuff..but i am not able to do so..Am i using it in the right way and am i passing the parameters co

4条回答
  •  花落未央
    2021-01-31 11:07

    Avoid adding a constructor.

    Simply pass your paramters in the task execute method

    new BackgroundTask().execute(a, b, c); // can have any number of params
    

    Now your background class should look like this

    public class BackgroundTask extends AsyncTask {
    
        @Override
        protected Long doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            String a = arg0[0];
            String b = arg0[1];
            String c = arg0[2];
            //Do the heavy task with a,b,c
            return null;
        }
        //you can keep other methods as well postExecute , preExecute, etc
    
    }
    

提交回复
热议问题