android – AsyncTask中onPreExecute和doInBackground之间的延迟很大

我有一个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): Pre execute: 1354798705667
I/TVLauncher/NewWeatherActivity(21691): Pre executed: 1354798705713
I/TVLauncher/NewWeatherActivity(21691): Do in background:
1354798724063 I/TVLauncher/NewWeatherActivity(21691): Done in
background: 1354798724083 I/TVLauncher/NewWeatherActivity(21691): Post
execute: 1354798724083 I/TVLauncher/NewWeatherActivity(21691): Post
executed: 1354798725046

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

解决方法

您可能正在调用一个框架或某个也使用异步任务的库.这将表现在Android的更高版本的减速.

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

这可能会将延迟降低到可忽略的水平.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...