Java tutorial
//package com.java2s; /** * @param context The {@link Context} to use. * @return An {@link AlertDialog} used to show the open source licenses used * in Apollo. */ import android.annotation.SuppressLint; import android.os.AsyncTask; import android.os.Build; import java.lang.ref.WeakReference; public class Main { /** * Execute an {@link AsyncTask} on a thread pool * * @param <T> Task argument type * @param forceSerial True to force the task to run in serial order * @param task Task to execute * @param args Optional arguments to pass to * {@link AsyncTask#execute(Object[])} */ @SuppressLint("NewApi") public static <T> WeakReference<AsyncTask<T, ?, ?>> execute(final boolean forceSerial, final AsyncTask<T, ?, ?> task, final T... args) { final WeakReference<AsyncTask<T, ?, ?>> taskReference = new WeakReference<AsyncTask<T, ?, ?>>(task); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) { throw new UnsupportedOperationException("This class can only be used on API 4 and newer."); } try { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || forceSerial) { taskReference.get().execute(args); } else { taskReference.get().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args); } } catch (Exception e) { e.printStackTrace(); } return taskReference; } }