All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.adtrace.sdk.scheduler.AsyncTaskExecutor Maven / Gradle / Ivy

There is a newer version: 2.6.1
Show newest version
package io.adtrace.sdk.scheduler;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public abstract class AsyncTaskExecutor {

    protected abstract Result doInBackground(Params[] params);

    protected void onPreExecute() { }

    protected void onPostExecute(Result result) { }

    @SafeVarargs
    public final AsyncTaskExecutor execute(final Params ... params) {
        onPreExecute();

        final Handler handler = new Handler(Looper.getMainLooper());
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                final Result result = doInBackground(params);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onPostExecute(result);
                    }
                });
            }
        });

        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy