Android - Async Task behavior in 2.3.3 and 4.0 OS

不羁岁月 提交于 2019-11-28 11:31:32
Ponyets

Are you useing AsyncTask. After Android 3.0, the default behavior of AsyncTask is execute in a single thread using SERIAL_EXECUTOR.

If you want AsyncTask run concurrently on any system version, you may use this code.

AsyncTask task = new YourTask();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
    task.execute(params);
} else {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}

Pre OS 1.6 - Multiple Async Tasks gets executed in sequence. OS 1.6 till OS 2.3 - Async Tasks run in parallel. From 3.0 - Again, Async Tasks gets executed in sequence.

Are you using an AsyncTask to execute the background operation? I think there is a difference between the implementation of the AsyncTask between GB and ICS.

Try to add some debug logging when the thread finishes its work and see if there is a difference between the two versions.

You can use the AsyncTaskCompat.executeInParallel for API < 11, you find this class in the appcompat v4 library.

An exemple of use :

AsyncTaskCompat.executeParallel(new AsyncTask<Void, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(Void... params) {
            return MediaStore.Images.Thumbnails.getThumbnail(
                    imageView.getContext().getContentResolver(),
                    id,
                    MediaStore.Images.Thumbnails.MINI_KIND,
                    null);
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
            if (bitmap != null) {
                // Add the image to the memory cache first
                CACHE.put(id, bitmap);
                if (listener != null) {
                    listener.onImageLoaded(bitmap);
                }
            }
        }
    });

enjoy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!