3

I'm trying to run them two AsyncTask sequences and does not work, it only runs the first. If I change the order of the AsyncTask, as only runs the first, the second is not, then the TimerTask if running. Look at my code: (Excuse my language, I speak Spanish.)

 public class ServicioPrincipal extends Service
    { ...
        public int onStartCommand(Intent intent, int flags, int startId)
        {
        new ServerAsyncTask().execute(serverSocket, getApplicationContext());
        new ClientEspejoAsyncTask().execute(getApplicationContext());
        timerTask = new TimerTask()
                        {
                            @Override
                            public void run()
                            {
                                //Log.i(TAG, "Ejecutando la tarea del servicio");
                                Utilitario.escribirLog("ServicioPrincipal: iniciarServicioPrincipal: timer is running...");
                            }
                        };
            try
            {
                timer.scheduleAtFixedRate(timerTask, 0, 30000); //, 0, 15000);
            }
            catch(Exception ex)
            {
                Utilitario.escribirLog("ServicioPrincipal: iniciarServicioPrincipal: catch:"+ex.getMessage());
            }
          }
      ...
    }

My classes:

private class ServerAsyncTask extends AsyncTask<Object, Void, String>
{
    @Override
    protected String doInBackground(Object... objs)
    {
        try
        {
            Utilitario.escribirLog("ServicioPrincipal: ServerAsyncTask: doInBackground");
            server = new Server((ServerSocket)objs[0], (Context)objs[1]);
            server.iniciarServer();
            return "1";
        }
        catch (Exception e)
        {
            Utilitario.escribirLog("ServicioPrincipal: ServerAsyncTask: doInBackground: catch:"+e.getMessage());
            return "0";
        }
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result)
    {
        Utilitario.escribirLog("ServicioPrincipal: ServerAsyncTask: onPostExecute-"+result);
        pararServicioPrinipal();
    }
}

private class ClientEspejoAsyncTask extends AsyncTask<Object, Void, String>
{
    @Override
    protected String doInBackground(Object... objs)
    {
        // params comes from the execute() call: params[0] is the url.
        try
        {
            Utilitario.escribirLog("ServicioPrincipal: ClientEspejoAsyncTask: doInBackground");
            clientEspejo = new ClientEspejo((Context)objs[0]);
            clientEspejo.iniciarClientEspejo();
            return "1";
        }
        catch (Exception e)
        {
            Utilitario.escribirLog("ServicioPrincipal: ClientEspejoAsyncTask: doInBackground: catch:"+e.getMessage());
            return "0";
        }
    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result)
    {
        Utilitario.escribirLog("ServicioPrincipal: ClientEspejoAsyncTask: onPostExecute-"+result);
        pararServicioPrinipal();
    }
}

thank you very much in advance.

Heberth
  • 615
  • 6
  • 16

2 Answers2

2

Put the Second AsyncTask on inside the onPostExecute of the First AsyncTask :

AsyncTask1 onPostExecute {
   new AsyncTask2().execute();
}

That should do it, good luck ^^

reidzeibel
  • 1,622
  • 1
  • 19
  • 24
  • It is a good idea but does not work for this case, because my first AsyncTask OnPostExecute only runs when doInBackground finishes running, I need AsyncTask both run at the same time. Excuse my language, I speak Spanish. – Heberth May 28 '13 at 02:00
  • 1
    No worries about language ^^, about running Multiple AsyncTask at the same time, you can check this post http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – reidzeibel May 28 '13 at 03:19
  • Thank you very much, I served for my I could muxo and Problem Solving. – Heberth May 28 '13 at 15:46
1

I haven't done that kind in AsyncTask but you can try perform the second task inside the doInBackground{} of the first task.

@Override
protected String doInBackground(Object... objs)
{
 .
 .
 .
 new ClientEspejoAsyncTask().execute(objs[0]);
}

but i would recommend you to use Thread instead for this kind of concept. http://www.tutorialspoint.com/java/java_thread_synchronization.htm

She Smile GM
  • 1,322
  • 1
  • 11
  • 33