重复AsyncTask

时间:2013-08-21 13:40:36

标签: android android-asynctask

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

3 个答案:

答案 0 :(得分:9)

你不能重复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);
    }
}

答案 1 :(得分:1)

您不能重复使用与according to the AsyncTask Docs

相同的AsyncTask对象
  

该任务只能执行一次(如果尝试第二次执行则会抛出异常。)

但是你可以在循环中创建你需要的那个类的许多新对象。但是,更好的方法是在doInBackground()内多次执行下载操作。

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

答案 2 :(得分:0)

我这样做了。它可以尝试直到(尝试== 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
    }

}