开发者

Asynchronous socket communication in Android with threads

开发者 https://www.devze.com 2023-01-23 04:11 出处:网络
I managed to programm an app that communicates with a server. I can write and I can read. I even managed to do it wit开发者_Go百科h Java NIO.

I managed to programm an app that communicates with a server. I can write and I can read. I even managed to do it wit开发者_Go百科h Java NIO.

My problem is that I have a endless while loop that is listening for new data to read. It blocks the whole program and I can't write anymore data.

I need a solution so the loop keeps running in background listening for new data to read while I send data.

Any suggestions?


Use AsyncTask. It was created just for this kind of jobs (doing long running background tasks, while still have a possibility to update UI).


You could either create a separate thread to handle the socket asynchronously and pass messages through a handler, or create a local service to handle the comms.

You should never do a long wait on the main (UI) thread.


If your loop is blocking the whole program there is something wrong with it. I don't see what other answer you can realistically expect until you post some code.

0

精彩评论

暂无评论...
验证码 换一张
取 消