重复的AsyncTaskAsyncTask

2023-09-06 02:57:00 作者:曲终人再见

我有一个关于在Android的应用程序重复的AsyncTask的可能性怀疑。我想重复一些操作,一个文件从服务器例如下载,n次如果不可能出于某些原因下载该文件。有一种快速的方法来做到这一点?

I have a doubt about the possibility of repeating an AsyncTask in an application for Android. I would like to repeat some operations, the download of a file from a server for example, n times if it is impossible for some reasons download the file. There is a quick way to do this?

推荐答案

您不能重复一个AsyncTask的但是你能重复一次执行的操作。

You cannot repeat an AsyncTask but you could repeat the operations it executes.

我做了,你可能要在地方的AsyncTask的扩展这个小助手类,唯一最大的区别是,你会使用,而不是doInBackground和onPostExecute repeatInBackground将有一个新的参数,最终引发的异常。

I've made this little helper class that you might want to extend in place of AsyncTask, the only big difference is that you will use repeatInBackground instead of doInBackground and that onPostExecute will have a new parameter, the eventual Exception thrown.

里面的任何repeatInBackground将被自动重复,直到结果是空不同的/没有抛出异常,并有过低于maxTries。

Anything inside repeatInBackground will be repeated automatically until result is different from null / exception is not thrown and there are been less than maxTries.

抛出循环中的最后一个例外,将在onPostExecute(结果,异常)返回。

The last exception thrown inside the loop will be returned in the onPostExecute(Result, Exception).

您可以设置最大尝试用RepeatableAsyncTask(IN​​T重试)构造函数。

You can set max tries using the RepeatableAsyncTask(int retries) constructor.

public abstract class RepeatableAsyncTask<A, B, C> extends AsyncTask<A, B, C> {
    private static final String TAG = "RepeatableAsyncTask";
    public static final int DEFAULT_MAX_RETRY = 5;

    private int mMaxRetries = DEFAULT_MAX_RETRY;
    private Exception mException = null;

    /**
     * Default constructor
     */
    public RepeatableAsyncTask() {
        super();
    }

    /**
     * Constructs an AsyncTask that will repeate itself for max Retries
     * @param retries Max Retries.
     */
    public RepeatableAsyncTask(int retries) {
        super();
        mMaxRetries = retries;
    }

    /**
     * Will be repeated for max retries while the result is null or an exception is thrown.
     * @param inputs Same as AsyncTask's
     * @return Same as AsyncTask's
     */
    protected abstract C repeatInBackground(A...inputs);

    @Override
    protected final C doInBackground(A...inputs) {
        int tries = 0;
        C result = null;

        /* This is the main loop, repeatInBackground will be repeated until result will not be null */
        while(tries++ < mMaxRetries && result == null) {
            try {
                result = repeatInBackground(inputs);
            } catch (Exception exception) {
                /* You might want to log the exception everytime, do it here. */
                mException = exception;
            }
        }
        return result;
    }

    /**
     * Like onPostExecute but will return an eventual Exception
     * @param c Result same as AsyncTask
     * @param exception Exception thrown in the loop, even if the result is not null.
     */
    protected abstract void onPostExecute(C c, Exception exception);

    @Override
    protected final void onPostExecute(C c) {
        super.onPostExecute(c);
        onPostExecute(c, mException);
    }
}