繁体   English   中英

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()

同步 VS 异步

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

我想您不应该在 AsyncTask 上浪费时间,因为它已被弃用

相反,你应该专注于谷歌推荐协程,或者其他一些艺术框架的 state 来实现你想要的(例如 rx java)

暂无
暂无

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

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