AsyncTask没有调用onProgressUpdate,onPostExecute

时间:2013-12-18 12:02:14

标签: android android-asynctask

我正在尝试了解Android上AsyncTask的用法,为此,我开发了这个简单的代码。

令人惊讶的是doInbackground被正确调用并且我看到了日志,但是没有调用其他两个方法。

我错过了什么吗?

感谢advancde

  public class DownloadFilesTask extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... params) {
            try {
                Log.i("Thread","1");
                Thread.sleep(1000);
                Log.i("Thread","2");
                Thread.sleep(1000);
                Log.i("Thread","3");
                Thread.sleep(1000);
                Log.i("Thread","4");
                Thread.sleep(1000);
                Log.i("Thread","5");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Void... progress) {
            Log.i("Thread","onProgress");
        }

        protected void onPostExecute(Void... result) {
            Log.i("Thread","onPosts");
        }
    }

[编辑]

使用此代码,除onProgressUpdate

外,所有工作正常
public class DownloadFilesTask extends AsyncTask<Void, Integer, String> {

public String doInBackground(Void... params) {
    try {
        int i = 0;
        Log.i("Thread","1");
        Thread.sleep(1000);
        publishProgress(i++);
        Log.i("Thread","2");
        Thread.sleep(1000);
        publishProgress(i++);
        Log.i("Thread","3");
        Thread.sleep(1000);
        Log.i("Thread","4");
        Thread.sleep(1000);
        Log.i("Thread","5");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "done";
}

public void onProgressUpdate(int progress) {
    Log.i("Thread","onProgress " + progress);
}

public void onPostExecute(String result) {
    Log.i("Thread","onPosts " + result);
}

在这里你可以看到我的LogCat的截图

Log

2 个答案:

答案 0 :(得分:5)

方法签名错误。它应该看起来像:

protected void onPostExecute(Void result) {
    Log.i("Thread","onPosts");
}

另外,我建议您使用@Override注释。这样,如果您尝试覆盖错误的方法,则会出现编译时错误。

编辑: 您发布的onProgressUpdate的签名是错误的。它应该是:

protected void onProgressUpdate(Integer... progress){}

答案 1 :(得分:1)

AsyncTask具有onProgressUpdate(Void ...)方法,您可以通过调用publishProgress()来调用每个迭代,例如每次在doInBackground()期间完成进度。

有关详细信息,请参阅docs

所以它应该看起来像这样:

public class DownloadFilesTask extends AsyncTask<Void, Integer, String> {

    protected String doInBackground(Void... params) {
        try {
            int i = 0;
            Log.i("Thread","1");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","2");
            Thread.sleep(1000);
            publishProgress(i++);
            Log.i("Thread","3");
            Thread.sleep(1000);
            Log.i("Thread","4");
            Thread.sleep(1000);
            Log.i("Thread","5");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "done";
    }

    protected void onProgressUpdate(int progress) {
        Log.i("Thread","onProgress " + progress);
    }

    private void onPostExecute(String result) {
        Log.i("Thread","onPosts " + result);
    }
}