Reputation: 34507
I am using the AsyncTask
to do network operation and set the minSDKVersion
to 8. I am trying to execute the AsyncTask
like how, it gives red line error then I need to clean the project everytime then it works.
downloadAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
To avoid these errors What I can do for this ?
Here see the screenshot what is the use of these @SuppressLint, @TargetAPI,
Thanks in advance.
Upvotes: 1
Views: 1717
Reputation: 1007296
That method (executeOnExecutor()
) and that field (THREAD_POOL_EXECUTOR
) were added in API Level 11. Your minSdkVersion
is 9, and so some devices that you run on will not have that method or field, and you will crash.
Either raise your minSdkVersion
to 11, or only use executeOnExecutor()
on new-enough devices:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
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);
}
}
Upvotes: 6