Android AsyncTask Run execute(final boolean forceSerial, final AsyncTask task, final T... args)

Here you can find the source of execute(final boolean forceSerial, final AsyncTask task, final T... args)

Description

Execute an AsyncTask on a thread pool

License

Apache License

Parameter

Parameter Description
forceSerial True to force the task to run in serial order
task Task to execute
args Optional arguments to pass to AsyncTask#execute(Object[])
T Task argument type

Declaration

@SuppressLint("NewApi")
public static <T> void execute(final boolean forceSerial,
        final AsyncTask<T, ?, ?> task, final T... args) 

Method Source Code

//package com.java2s;
/*//from   w w  w.  java  2  s .c om
 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import android.annotation.SuppressLint;

import android.os.AsyncTask;
import android.os.Build;

public class Main {
    /**
     * Execute an {@link AsyncTask} on a thread pool
     * 
     * @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[])}
     * @param <T> Task argument type
     */
    @SuppressLint("NewApi")
    public static <T> void execute(final boolean forceSerial,
            final AsyncTask<T, ?, ?> task, final T... args) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.DONUT) {
            throw new UnsupportedOperationException(
                    "This class can only be used on API 4 and newer.");
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
                || forceSerial) {
            task.execute(args);
        } else {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args);
        }
    }
}

Related

  1. execute(final boolean forceSerial, final AsyncTask task, final T... args)
  2. executeAsyncTask(AsyncTask task, T... params)
  3. executeAsyncTask(AsyncTask task, T... params)
  4. runSuCommandAsync(String command)