简体   繁体   中英

Android: how does the execute() method in AsyncTask work?

So I am currently learning about AsyncTask in Android and there is the following simple example:

public class MainActivity extends AppCompatActivity {

    public class DownloadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);
            ...
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask task = new DownloadTask();
        String result = null;
        try {
            result = task.execute("http://someURL").get();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.i("Result",result);
    }
}

The only thing I don't understand about the example is the following:

In the try section there is a string being passed to the execute method. The execute method is part of the task object. The doInBackground method is also part of the task object. How does the doInBackground method know about the content that I am passing to the execute method? Because in the tutorial I learned, that the url = new URL(urls[0]); contains the info of the string that is passed through the execute method. Can someone explain this connection to me?

The class DownloadTask is a direct child class of AsyncTask. AsyncTask provides an implementation to manage a task in async way and you don't need to worry about any details about thread pool or any other details. AsyncTask class uses Java generics, ie a template with three parameters. You are using String as first and third parameter of the template, so it means doInBackground will take a variant number of String objects and it will return a String object. The "connection" between execute method and doInBackground is inside AsyncTask class and it's hidden in the parent class, it's the encapsulation and information hiding usually performed in object oriented design.

public class DownloadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {

            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(urls[0]);
            ...
        }
    }

Here is just an overview of how it works. I poorly mimicked its work but hopefully, you would get an idea.

fun main() {
  object : AsyncTask() {
     override fun doInBackground() {
         println("I am now in the background thread ^_^")
      }
  }.execute()
}



abstract class AsyncTask() {

  abstract fun doInBackground()

  fun execute() {
      Thread(Runnable { doInBackground() }).apply {  //Don't ever use a thread like this. It's just an example!
          name = "Background thread"
      }.start()
   }
}

See, how I provided my code that needs to work in the background thread in overriden doInBackground() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM