execute AsyncTask and catch RejectedExecutionException - Android android.os

Android examples for android.os:AsyncTask

Description

execute AsyncTask and catch RejectedExecutionException

Demo Code


import java.util.concurrent.RejectedExecutionException;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;

public class Main {

  @SuppressLint("NewApi")
  public static <T> void executeTask(AsyncTask<T, ?, ?> task, T... params) {
    try {/*from   w w  w  . jav a 2 s  .  c  o m*/
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
      } else {
        task.execute(params);
      }

    } catch (RejectedExecutionException e) {
      executeTask(task, params);
    } catch (IllegalStateException e) {
    }
  }
}

Related Tutorials