3

2 つの AsyncTask シーケンスを実行しようとしていますが、機能しません。最初のシーケンスのみが実行されます。AsyncTask の順序を変更すると、最初のタスクのみが実行され、2 番目のタスクは実行されず、実行されている場合は TimerTask が実行されます。私のコードを見てください: (すみません、私はスペイン語を話します。)

 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());
            }
          }
      ...
    }

私のクラス:

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();
    }
}

事前にどうもありがとうございました。

4

2 に答える 2

2

onPostExecute最初の AsyncTask の内側に 2 番目の AsyncTask を配置します。

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

それでうまくいくはずです、頑張ってください^^

于 2013-05-28T01:41:40.197 に答える