0

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

Bram
  • 4,533
  • 6
  • 29
  • 41
  • 2
    You might consider checking out [Google Play Game Services Real-time Multiplayer](https://developers.google.com/games/services/android/multiplayer) as it does much of the work for you, including linking players together and handling message sending/receiving. – ianhanniballake Jun 01 '13 at 22:13

1 Answers1

0

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

msh
  • 2,700
  • 1
  • 17
  • 23
  • In my understanding does the service class not run in a seperate thread. Thanks for the respone though. – Bram Jun 05 '13 at 17:46
  • 1
    Service class doesn't run. Service functions can run on the UI thread, on binder threads, you can start async task, create a handler with your own looper thread, there are many options. Don't use AsyncTask for long processing though - they all share a small thread pool (or just 1 thread depending on version) – msh Jun 05 '13 at 17:51