Do AsyncTask as Callable - Android android.os

Android examples for android.os:AsyncTask

Description

Do AsyncTask as Callable

Demo Code


import android.os.AsyncTask;

public class Main{
    //from  w ww.  ja v  a 2s .co  m
    public static <T> void doAsync(final Callable<T> callable,
            final Callback<T> callback) {
        new AsyncTask<Void, Void, T>() {

            @Override
            protected T doInBackground(Void... params) {
                try {
                    return callable.call();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(T result) {
                callback.onCallback(result);
            }
        }.execute((Void[]) null);
    }
}

Related Tutorials