Search code examples
androidmultithreadingandroid-asynctaskmultiplayerbackground-thread

Android multi-threading connection game


I'm creating a multiplayer game. Now I came to the point that I need to fetch and keep track of all the incoming server messages on the client side. I saw an AsynTask class to do background work for me. Do you think this is the way to go to keep track of all the messages and pass them around to different activities or is there a better way to do this?

How I see this:

//more or less pseudo-code

public class .... extends AsyncTask {

    Connection connection = new Connection();
    connection.setUpConnection();

    doInBackground() {
          processIncomingMessages();
          processOutgoingMessages();
    }
}

Also, if I create this AsynTask to keep track of my messages, where do I need to create this? Perhaps in the onCreate of my first activity?

Thanks in advance for the help!

Kind regards, Bram


Solution

  • For background operations that you want to use from multiple activities within the same application, you can use local service. See an example