简体   繁体   English

Android:AsyncTask 中的 execute() 方法是如何工作的?

[英]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:所以我目前正在学习 Android 中的 AsyncTask 并且有以下简单示例:

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.try部分中,有一个字符串被传递给execute方法。 The execute method is part of the task object. execute方法是task object 的一部分。 The doInBackground method is also part of the task object. doInBackground方法也是task object 的一部分。 How does the doInBackground method know about the content that I am passing to the execute method? doInBackground方法如何知道我传递给execute方法的内容? Because in the tutorial I learned, that the url = new URL(urls[0]);因为在教程中我了解到, url = new URL(urls[0]); contains the info of the string that is passed through the execute method.包含通过execute方法传递的字符串的信息。 Can someone explain this connection to me?有人可以向我解释这种联系吗?

The class DownloadTask is a direct child class of AsyncTask. class DownloadTaskAsyncTask. 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提供了一种以异步方式管理任务的实现,您无需担心有关线程池的任何细节或任何其他细节。 AsyncTask class uses Java generics, ie a template with three parameters. AsyncTask class 使用 Java generics,即一个具有三个参数的模板。 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.您使用 String 作为模板的第一个和第三个参数,因此这意味着 doInBackground 将采用不同数量的String对象,并将返回一个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. execute方法和doInBackground之间的“连接”在AsyncTask class内部,隐藏在父级class中,是ZA8CFDE6331BD59EB2AC96F8911C4B66666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666

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.看,我是如何在重写的doInBackground()方法中提供需要在后台线程中工作的代码的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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