Asynchronous task .. cannot call executeOnExecutor ()

I have a little problem with some Async tasks that are running in my Android app. Due to the fact that I'm using some kind of network IO, it may someday take longer than expected and block the execution of other async tasks.

I need to keep my target and minimum sdk versions, but they are aimed at targetSdkVersion = "15" with minSdkVersion = "8". I require that when I call the Async task, I can check the SDK of the devices and, if it is more than 11, it can call executeOnExecutor (), and not just execute, to allow the device to run them in parallel and prevent this blocking operation.

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

However, when called:

MyAsyncTask(this).executeOnExecutor(); 

I get the error message โ€œThe executeOnExecutor () method 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, and onPostExecute.

I tried following the tips given here ... http://steveliles.imtqy.com/android_s_asynctask.html

+4
source share
1 answer

Set the build target for API level 11 or higher. Please note that "build target"! = "Target SDK" ( android:targetSdkVersion ). The build target is set in Project> Properties> Android on Eclipse or project.properties to build the command line.

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); } } 

Then you can use this method to start your tasks:

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

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

+11
source

Source: https://habr.com/ru/post/1483540/


All Articles