How to pass variables in and out of AsyncTasks?

前端 未结 5 1132
深忆病人
深忆病人 2020-12-01 09:39

I haven\'t spent much time working with AsyncTasks in Android. I\'m trying to understand how to pass variables to and from the class. The syntax:



        
5条回答
  •  盖世英雄少女心
    2020-12-01 10:02

    Passing a simple String:

     public static void someMethod{ 
         String [] variableString= {"hello"};
         new MyTask().execute(variableString);
    }
    
    static class MyTask extends AsyncTask {
    
            // This is run in a background thread
            @Override
            protected String doInBackground(String... params) {
                // get the string from params, which is an array
                final String variableString = params[0];
    
                Log.e("BACKGROUND", "authtoken: " + variableString);
    
                return null;
            }
        }
    

提交回复
热议问题