Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async task.. cannot call executeOnExecutor()

I have a bit of an issue with some Async tasks that are running in my android application. Due to the fact I am using some network IO, it can sometime's take longer than expected and block other async tasks running.

I need to keep my target and min sdk versions as they are but they are targeting targetSdkVersion="15" with a minSdkVersion="8". I require it so that when an Async task is called, I can check the devices SDK and if is greater than 11 it can call executeOnExecutor() as opposed to just execute to allow the device to run these in parallel and prevent this blocking operation.

Although I have a target SDK of 15, the device I am using has an SDK of 17.

However when calling:

MyAsyncTask(this).executeOnExecutor();

I get an error "The method executeOnExecutor() is undefined for the type" and the only option available to me is:

MyAsyncTask(this).execute();

MyAsyncTask is a class object that extends AsyncTask and overloads the standard methods onPreExecute, doInBackground, onProgressUpdate & onPostExecute.

I tried following some of the advice listed here... http://steveliles.github.io/android_s_asynctask.html

like image 669
Joey Bob Avatar asked May 30 '13 09:05

Joey Bob


1 Answers

Set your build target to API Level 11 or higher. Note that "build target" != "target SDK" (android:targetSdkVersion). "Build target" is set in Project > Properties > Android on Eclipse, or project.properties for command-line builds.

For conditional use of executeOnExecutor(), another approach is to use a separate helper method:

  @TargetApi(11)
  static public <T> void executeAsyncTask(AsyncTask<T, ?, ?> task,
                                          T... params) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
    }
    else {
      task.execute(params);
    }
  }

You would then use this method to kick off your tasks:

 executeAsyncTask(new MyTask(), param1, param2);

(for however many parameters you wanted to pass to execute())

like image 79
CommonsWare Avatar answered Oct 25 '22 13:10

CommonsWare