简体   繁体   中英

Android AsyncTask in another AsyncTask

I want to run an AsyncTask in another AsyncTask with this code:

   protected Integer doInBackground(Void... params) {
    InetAddress inetAddress;
    try {
        while (true) {
            inetAddress = InetAddress.getByName("192.168.1.1");
            mSocket = new Socket(inetAddress, 4444);

            SocketData data = new SocketData();


            GetPacket pack = new GetPacket();
            AsyncTask<SocketData, Integer, Integer> running = pack.execute(data);

but have an error : Method must be called from the main thread android current inferred thread is worker

Do it in the onProgressUpdate this method is running on the MainThread

protected Integer doInBackground(Void... params) {
    publishProgress(data);
}

protected void onProgressUpdate(SocketData... data) {
    GetPacket pack = new GetPacket();
    AsyncTask<SocketData, Integer, Integer> running = pack.execute(data[0]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM