AsyncTask如何一个进程到另一个进程?

问题描述 投票:0回答:2

我目前正在自学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的一些基本逻辑或流程。Plz,帮我解决这个心中未解的问题。示例代码如下图所示。先谢谢你了。

    @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();
        }
java android android-asynctask
2个回答
0
投票

我想你不应该在AsyncTask上浪费时间,因为它就是 弃用.

相反,你应该专注于coroutines,由google推荐的 此处 或者其他一些先进的框架来实现你想要的东西(例如:rx java)


0
投票

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

同步VS异步

您可以阅读 这个 的文章,以便更好地理解,尽管它是用Javascript来解释的。

© www.soinside.com 2019 - 2024. All rights reserved.