Example usage for android.os AsyncTask execute

List of usage examples for android.os AsyncTask execute

Introduction

In this page you can find the example usage for android.os AsyncTask execute.

Prototype

@MainThread
public static void execute(Runnable runnable) 

Source Link

Document

Convenience version of #execute(Object) for use with a simple Runnable object.

Usage

From source file:Main.java

/**
 * Execute an {@link AsyncTask} on a thread pool
 * /*from  w ww .j a va 2s .com*/
 * @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);
    }
}

From source file:Main.java

/**
 * Runs an AsyncTask. These are always executed in parallel, independently of the device's API level.
 * @param task Task to be run./*w w w  .  ja v a 2  s. com*/
 * @param params Task parameters.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static <Params> void executeAsyncTask(AsyncTask<Params, ?, ?> task, Params... params) {
    // By default, in Honeycomb and later AsyncTasks execute serially, while they execute
    // in parallel (using a thread pool) in earlier versions. Force parallelism here.
    if (isHoneycomb())
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
    else
        task.execute(params);
}

From source file:org.piwik.sdk.TrackerBulkURLProcessor.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static <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 {/*from w  ww.j a v a  2s.c  o m*/
        task.execute(params);
    }
}

From source file:com.kasungunathilaka.sarigama.util.PlayerQueue.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB) // API 11
public static <T> void executeAsyncTask(AsyncTask<T, ?, ?> asyncTask, T... params) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
    else//  w  w  w  .j  ava2s .c o m
        asyncTask.execute(params);
}

From source file:cloud4all.Utils.NetworkAnode.java

public static void sendRequest(String anode) {
    AsyncTask<String, Void, Void> at = new AsyncTask<String, Void, Void>() {
        @Override//  w  w  w  . j av a  2s. c o m
        protected Void doInBackground(String... url) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet command = new HttpGet(url[0]);
                HttpResponse response = client.execute(command);

                InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
                BufferedReader in = new BufferedReader(isr);
                String res = in.readLine();
                Log.d("GPIIUserListeners", "GPII has returned: " + res);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    at.execute(anode);
}

From source file:conexionSiabra.PeticionPost.java

public String Ejecutar() {

    AsyncTask<ArrayList<NameValuePair>, Void, String> variable = new PeticionHttp();
    ((PeticionHttp) variable).setURL(url);
    variable.execute(listaDePares);
    try {// www . j  a v a 2s.  c  o m
        result = variable.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:com.edmondapps.utils.android.fragment.AbstractAsyncFragment.java

private void onExecuteAsyncTask(AsyncTask<Params, Progress, Result> asyncTask) {
    if (mAsyncTask != null) {
        mAsyncTask.cancel(false);/*from ww w. jav a 2  s .c  om*/
    }
    mAsyncTask = asyncTask;
    asyncTask.execute(onCreateAsyncTaskParams(asyncTask));
}

From source file:edu.ucdavis.glass.sepsis.support.OverviewActivity.java

private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetector = new GestureDetector(context);

    // create a base listener for generic gestures
    gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
        @Override/*from  w  w  w . j  a va  2 s . c o  m*/
        public boolean onGesture(Gesture gesture) {
            if (gesture == Gesture.TAP) {
                mAudioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);

                // set up AsyncTask
                AsyncTask<String, Void, JSONObject> JSON = new LoadJSONAsyncTask((Context) mContext,
                        "Updating all patient's info...", mContext);

                // run AsyncTask
                JSON.execute(Global.recentPatients.peek().id);
                return true;
            } else if (gesture == Gesture.SWIPE_RIGHT) {
                finish();
                mAudioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
                startActivity(new Intent(getApplicationContext(), VitalsActivity.class));
                return true;
            } else if (gesture == Gesture.SWIPE_LEFT) {
                finish();
                mAudioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
                startActivity(new Intent(getApplicationContext(), SupportActivity.class));
                return true;
            }
            return false;
        }
    });

    return gestureDetector;
}

From source file:com.upnext.blekit.BLEKit.java

private static void fetchJsonLocal() {
    L.d(".");// w ww.ja  va  2 s  . com
    AsyncTask<String, Void, JsonNode> task = new AsyncTask<String, Void, JsonNode>() {

        @Override
        protected JsonNode doInBackground(String... params) {
            L.d("fetching from " + params[0]);
            HttpClient client = new HttpClient(params[0]);
            Response<JsonNode> response = client.get(JsonNode.class, null);
            return response.getBody();
        }

        @Override
        protected void onPostExecute(JsonNode s) {
            L.d("fetched " + s);
            if (s != null) {
                mCurrentZone = jsonParser.parse(s + "");
            }
        }
    };
    task.execute(jsonUrl);
}

From source file:org.benetech.secureapp.activities.CreatePassphraseActivity.java

@Override
protected void postMountStorageExecute() {
    dismissProgressDialog();//from   ww  w  .j a  va2 s.c om
    if (getMartusCrypto(getApplication()).hasKeyPair())
        return;

    showProgressDialog(getString(R.string.progress_dialog_message_creating_martus_crytpo_keypair));
    char[] passphrase = passphraseEditField.getEditText().getText().toString().toCharArray();
    try {
        final AsyncTask<Object, Void, Boolean> createAccountTask = new CreateMartusCryptoKeyPairTask(
                getMartusCrypto(getApplication()), mCreateMartusCryptoKeyPairCallback, getSettings());

        char[] pw = Utility.convertToCharArray(getCacheWordActivityHandler().getEncryptionKey());
        createAccountTask.execute(new Object[] { pw });
    } catch (Exception e) {
        Log.e("uploadForm", getString(R.string.error_message_failed_to_create_account), e);
    } finally {
        Wiper.wipe(passphrase);
    }
}