2

I am not sure if this is possible or if I am thinking of this correctly.

In the Android developer training for Displaying Bitmaps Efficiently, a custom version of the AsyncTask is used to make the code compatible with android versions below HONEYCOMB.

This great answer explains the differences between AsyncTask operation in pre and post HONEYCOMB. The reason that a custom version is supplied in the guide is that the default in pre HONEYCOMB (a pool of tasks) will very easily crash with a java.util.concurrent.RejectedExecutionException if the GridView of images is scrolled quickly. For this scenario, the default post HONEYCOMB version (serial execution on an additional thread) is what is required.

I could easily do this by using a newer version of AsyncTask in my project (like done in the developer training example) but this is not very compatible with future changes.

What I would like to do is the following:

If the version is HONEYCOMB or greater, use the implementation of AsyncTask provided in the framework, whereas if it is earlier, use the AsyncTask implementation provided in my application.

Is this possible (and how) or should I be thinking about it differently?

EDIT: I understand how to provide conditionals based on the running SDK_VERSION.

The issue I have is that AsyncTask is an abstract class, meaning that I have to provide an implementation. E.g. I have a AsyncTask called myTask which extends AsyncTask like below:

public class myTask extends AsyncTask<Void, Void, Void> {
    // Implementations for the abstract functions (doInBackground)
}

How do I decide which version of the AsyncTask is extended?

Community
  • 1
  • 1
btalb
  • 6,937
  • 11
  • 36
  • 44

2 Answers2

2

You can do something like:

if(android.os.Build.VERSION.SDK_INT >= 11){ // Honeycomb
{
  AsyncTask taskObj = new DefaultAsyncTaskImpl();
  taskObj.execute();  
} else {
  AsyncTask taskObj = new CustomAsyncTaskImpl();
  taskObj.execute();
}

And in your code define both classes in which the first extends AsyncTask and the second is your own implementation

Note: SDK_INT available in API 4+

Edit:

You can use factory design pattern to decide which object to create based on the SDK version.

iTech
  • 18,192
  • 4
  • 57
  • 80
  • See edit. That suggests what I am fearing, providing separate implementations for every `AsyncTask` class in my entire application. Also, won't that code sample return two different Objects so all code would have to handle cases for different object types? – btalb Mar 15 '13 at 03:45
  • *for every custom `AsyncTask` class (like `myTask`) is what I meant – btalb Mar 15 '13 at 03:59
  • You can use `Factory design pattern` to decide which object to create based on the SDK version. – iTech Mar 15 '13 at 04:02
  • Thanks! The `Factory design pattern` is exactly what I am looking for. If at all possible could you please include that in the answer so other people can find it if they are looking? Thanks again – btalb Mar 15 '13 at 04:33
1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Use built in AysncTask
        } else {
            // Use your AsyncTask
        }

you need to check the OS version

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77