编程语言
首页 > 编程语言> > java-如何一个接一个地运行AsyncTask?

java-如何一个接一个地运行AsyncTask?

作者:互联网

我正在尝试为它们运行两个AsyncTask序列,但不起作用,它仅运行第一个.如果我更改了AsyncTask的顺序,因为它仅运行第一个,而第二个则不运行,则如果运行,则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();
    }
}

提前非常感谢您.

解决方法:

将第二个AsyncTask放在第一个AsyncTask的onPostExecute内:

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

那应该做的,祝你好运^^

标签:android-asynctask,java,android
来源: https://codeday.me/bug/20191123/2066121.html