3

I am currently working on an android application that has to handle a network connection using several AsyncTasks.

This is the first task that is establishing the connection and calling a new task which is handling the microphone input.

private class establishConnectionTask extends
        AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            // initialize connection
            initConnection();
            MicrophoneTask micTask = new MicrophoneTask();
            micTask.execute("");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Executed";
    }

    @Override
    protected void onPostExecute(String result) {
        mReadInputTask = new readInputTask();
        mReadInputTask.execute("");
        super.onPostExecute(result);
    }

Everything works fine, the connection is working and I can transfer data. Also the MicrophoneTask is doing it's job.

Here comes the problem:

In the onPostExecute method I am creating a new AsyncTask which should handle all the network input.

This is how the readInputTask looks like:

private class readInputTask extends AsyncTask<String, Integer, String>
{

    @Override
    protected void onPreExecute()
    {
        Log.d("DEBUG", "pre");
        super.onPreExecute();

    }

    @Override
    protected String doInBackground(String... params) {
        // blocking readInput method
          Log.d("DEBUG", "doInBackground");
    }


    @Override
    protected void onPostExecute(String result) {
        Log.d("DEBUG", "post");
        super.onPostExecute(result);
    }
}

The readInputTask somehow gets stuck in the onPreExecute method of the readInputTask. The only output I get is "pre", eventhough I also expect "doInBackground" and "post".

Does anyone see an error or knows a solution for this?

Any help is appreciated!

Al0x
  • 917
  • 2
  • 13
  • 30

1 Answers1

2
mReadInputTask.execute("");

When you use AsyncTask#execute(params), the AsyncTasks are executed serially: one after the other. To execute AsyncTasks in parallel, use AsyncTask#executeOnExecutor(...).

From the docs on executeOnExecutor (Executor exec, Params... params):

This method is typically used with THREAD_POOL_EXECUTOR to allow multiple tasks to run in parallel on a pool of threads managed by AsyncTask, however you can also use your own Executor for custom behavior.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thanks alot, got it working now! I thought AsyncTasks were parallel. – Al0x Sep 23 '13 at 21:32
  • 1
    @Al0x Yea, they used to be. From version 1.6 (Donut) and up until 3.0 (Honeycomb), AsyncTask were executed in parallel by default (using a ThreadPool). But, `[s]tarting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution` (taken from [AsyncTask(subheading: Order of execution)](http://developer.android.com/reference/android/os/AsyncTask.html)). – Vikram Sep 23 '13 at 22:14