AsyncTask 如何将一个进程用于另一个进程?

How does AsyncTask work one process to another one?

我目前正在自学 android,对 java 还很陌生。我想知道 AsyncTask 是如何工作的:onPreExecute() -> doInBackground() -> onPostExecute()。当我查看其他人定义他们的 AsynTask 时,似乎在他们的代码中只声明了方法,没有调用该方法。我无法弄清楚 doInBackground() 是如何在 onPreExecute() 之后出现的,没有链接两者的代码,例如:

onPreExecute(){   ~~~~~ call doInBackground()}

我的观点是,当AsyncTask.execute()被调用时,onPreExecute()被调用,然后是doInBackground(),最后是onPostExecute()。我在库中找不到任何实际将它们连接在一起的代码。我只能找到这个:

@MainThread
    public final AsyncTask<Params, Progress, Result> execute(Params... params) {
        return executeOnExecutor(sDefaultExecutor, params);

@MainThread
    public final AsyncTask<Params, Progress, Result> executeOnExecutor(Executor exec,
            Params... params) {
        if (mStatus != Status.PENDING) {
            switch (mStatus) {
                case RUNNING:
                    throw new IllegalStateException("Cannot execute task:"
                            + " the task is already running.");
                case FINISHED:
                    throw new IllegalStateException("Cannot execute task:"
                            + " the task has already been executed "
                            + "(a task can be executed only once)");
            }
        }

        mStatus = Status.RUNNING;

        onPreExecute();

        mWorker.mParams = params;
        exec.execute(mFuture);

        return this;
    }

这里调用AsyncTask.execute()时,会调用onPreExecute()。但是与 doInBackground 没有任何联系,任务工作得很好。我觉得我缺少 java 或 android 的一些基本逻辑或过程。请帮我解决这个未解决的问题。示例代码如下所示。提前谢谢你。

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mLoadingIndicator.setVisibility(View.VISIBLE);
    }

    @Override
    protected String[] doInBackground(String... params) {

        /* If there's no zip code, there's nothing to look up. */
        if (params.length == 0) {
            return null;
        }

        String location = params[0];
        URL weatherRequestUrl = NetworkUtils.buildUrl(location);

        try {
            String jsonWeatherResponse = NetworkUtils
                    .getResponseFromHttpUrl(weatherRequestUrl);

            String[] simpleJsonWeatherData = OpenWeatherJsonUtils
                    .getSimpleWeatherStringsFromJson(MainActivity.this, jsonWeatherResponse);

            return simpleJsonWeatherData;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String[] weatherData) {
        // COMPLETED (19) As soon as the data is finished loading, hide the loading indicator
        mLoadingIndicator.setVisibility(View.INVISIBLE);
        if (weatherData != null) {
            // COMPLETED (11) If the weather data was not null, make sure the data view is visible
            showWeatherDataView();
            /*
             * Iterate through the array and append the Strings to the TextView. The reason why we add
             * the "\n\n\n" after the String is to give visual separation between each String in the
             * TextView. Later, we'll learn about a better way to display lists of data.
             */
            for (String weatherString : weatherData) {
                mWeatherTextView.append((weatherString) + "\n\n\n");
            }
        } else {
            // COMPLETED (10) If the weather data was null, show the error message
            showErrorMessage();
        }

是的,你是对的。逻辑是 onPreExecute() -> doInBackground() -> onPostExecute()

Synchronous VS asynchronous

你可以阅读 this 文章以获得更好的理解,即使它使用 Javascript 来解释它。

我猜你不应该在 AsyncTask 上浪费时间,因为它是 deprecated

相反,您应该关注 google here 推荐的协程,或其他一些最先进的框架来实现您想要的(例如 rx java)