6

我有一个 AsynkTask:

new AsyncTask<Void, Void, Void>() {

            private ProgressDialog mProgressDialog;

            @Override
            protected void onPreExecute() {
                Log.i(TAG, "Pre execute: " + System.currentTimeMillis());
                super.onPreExecute();

                mProgressDialog = ProgressDialog.show(NewWeatherActivity.this, null,
                        getResources().getString(R.string.weather_is_updating));
                mProgressDialog.setCancelable(false);

                Log.i(TAG, "Pre executed: " + System.currentTimeMillis());
            }

            @Override
            protected Void doInBackground(Void... voids) {
                Log.i(TAG, "Do in background: " + System.currentTimeMillis());
                // Some actions
                Log.i(TAG, "Done in background: " + System.currentTimeMillis());
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                Log.i(TAG, "Post execute: " + System.currentTimeMillis());
                super.onPostExecute(aVoid);

                mProgressDialog.dismiss();
                Log.i(TAG, "Post executed: " + System.currentTimeMillis());
            }
        }.execute();

日志:

I/TVLauncher/NewWeatherActivity(21691):预执行:1354798705667 I/TVLauncher/NewWeatherActivity(21691):预执行:1354798705713 I/TVLauncher/NewWeatherActivity(21691):在后台执行:1354798724063 I/TVLauncher/NewWeatherActivity(2166)在后台:1354798724083 I/TVLauncher/NewWeatherActivity(21691):执行后:1354798724083 I/TVLauncher/NewWeatherActivity(21691):执行后:1354798725046

因此,onPreExecute 和 doInBackground 之间的延迟大约为 19 秒。为什么?

4

1 回答 1

8

您可能正在调用也使用异步任务的框架或某些库。这将在更高版本的 Android 上表现出放缓。

AsyncTasks 现在按顺序运行。这意味着新的异步任务在最后一个任务完成之前不会运行。而不是调用执行尝试调用 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");

这可能会将延迟降低到可以忽略不计的水平。

于 2013-05-05T16:28:48.673 回答