Async类中的多个任务

时间:2014-05-28 07:51:08

标签: android android-asynctask

我需要在AsyncTask类中执行两个不同的函数: - 1)从服务器获取字符串数据。 2)从服务器下载图像。请建议我如何让它们同时运行。我尝试按照以下方式进行,但这不起作用。

class SignIn extends AsyncTask<String, String, String[]> {

            @Override
            protected String[] doInBackground(final String... params) 
            {
                  }


            protected Bitmap[] doInBackground(Bitmap...urls) 
                {
              }

1 个答案:

答案 0 :(得分:0)

首先,由于你的doInBackground()代码总是保持不变,我建议你把它移到一个通用的实用程序类中。

除此之外,您可以采用以下两种方式之一:

1) Create a new AsyncTask for each type of request that can call your utility class, and have its own onPostExecute()

2) Create one AsyncTask that has a flag in it, which can be checked in the onPostExecute() method to see what code needs to be executed there. You will have to pass the flag in in the constructor or as a parameter in execute.