6

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

4

3 回答 3

11

您不能重复 AsyncTask ,但可以重复它执行的操作。

我已经创建了这个小助手类,您可能想要扩展它来代替 AsyncTask,唯一的大区别是您将使用 repeatInBackground 而不是 doInBackground,并且 onPostExecute 将有一个新参数,最终抛出异常。

repeatInBackground 中的任何内容都将自动重复,直到结果与 null 不同/不抛出异常并且少于 maxTries。

循环内最后抛出的异常将在 onPostExecute(Result, Exception) 中返回。

您可以使用 RepeatableAsyncTask(int retries) 构造函数设置最大尝试次数。

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);
    }
}
于 2013-08-21T13:44:41.160 回答
1

根据 AsyncTask Docs ,您不能重复使用相同的AsyncTask对象

该任务只能执行一次(如果尝试第二次执行将引发异常。)

但是您可以在循环内创建所需的该类的许多新对象。但是,更好的方法是在doInBackground().

如果这不能回答您的问题,那么请更具体地说明您的问题

于 2013-08-21T13:45:24.700 回答
0

我就是这样做的。它可以不断尝试直到 (tries == MAX_RETRY) 或结果不为空。接受答案的稍微修改的代码,对我来说更好。

private class RssReaderTask extends AsyncTask<String, Void, ArrayList<RssItem>> {

    // max number of tries when something is wrong
    private static final int MAX_RETRY = 3;

    @Override
    protected ArrayList<RssItem> doInBackground(String... params) {

        ArrayList<RssItem> result = null;
        int tries = 0;

        while(tries++ < MAX_RETRY && result == null) {
            try {
                Log.i("RssReaderTask", "********** doInBackground: Processing... Trial: " + tries);
                URL url = new URL(params[0]);
                RssFeed feed = RssReader.read(url);
                result = feed.getRssItems();
            } catch (Exception ex) {
                Log.i("RssReaderTask", "********** doInBackground: Feed error!");
            }
        }

        return result;
    }

    @Override
    protected void onPostExecute(ArrayList<RssItem> result) {
        // deal with result
    }

}
于 2015-06-09T08:32:58.677 回答