5

What would be the best way to implement this. I have an Android app that will use my python server to allow communication between 2 phones in rounds. Rounds mean that they can't talk to each other until a round start and once they send a message they can't send another until the other person responds which will then start a new round.

I was thinking I would use the IntentService but it seems wrong to have the server constantly starting and stopping and I don't won't to have to worry about the issues with asynctask or is that the best way to handle it. How could I have a service that should receive and send messages to the client, seems services are more one way things?

4 Answers 4

16

Intent services are nothing more that worker threads that are triggered by intents, execute their actions in a separate thread and then get shut down. They are designed to be started and stopped.

If you need to perform stuff like an http get, or in any case interaction that do not require to stay connected to the server, use intent services and get your activities notified using broadcast events.

If your app needs to stay connected with the server (i.e. permanent tcp connection), the way I'd go for is to have a service (not an intent one) that performs the networking stuff using an asynctask or a more classic thread hosted in the service. You can then make the activity interact with the service using bindToService() .

I'd recommend not to use asynctasks inside an activity. You will risk to loose the server response in case of horizontal / vertical view changes, as oneilse14 stated in his reply.

2
5

I highly recommend the IntentService/Broadcast Receiver route. Avoiding the nasty configuration change issues associated with AsyncTask will make your life ten times easier.

2
  • I see, but won't a standard thread with messages do the trick as well? An activity will receive a message from the background thread even after a configuration change, won't it?
    – Yar
    Aug 25, 2012 at 6:47
  • 1
    The thread will hold on to the reference to the original Context. If you re-register your Context/handlers after the configuration change you can get around this, but the Receiver route is still easier.
    – SeanPONeil
    Aug 25, 2012 at 19:47
2

As far as i understood your problem is of type worker-queue model Producer-consumer model). Intentservices are meant to do that. You should use services if and only you need to do multithreading. You do can communicate with Activity and Service by using IBinder interface.

Asynctask are just a specialized threads so that you can update your UI easily. But for your case IntentService seems to be best option.

3
  • You are wrong: * Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. * A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). So, its not if and only if you need multithreading.
    – roiberg
    Mar 20, 2013 at 12:14
  • 1
    IntentService uses AsyncTask under the hood to handle the processing off the main thread, @roiberg.
    – lilbyrdie
    Apr 16, 2013 at 20:27
  • @roiberg Correct. Services will work on main thread . To make this answer more clear, Services will be useful for multithreading(threadpool) or keeping a serialized executor/handlerthread for very long time in the background.
    – manjusg
    Dec 28, 2015 at 10:14
0

I would use an Alarm, which is scheduled via the AlarmManager, as then it can be set to check if the round has started/turn. It has the advantages of a service but not the horrors of battery drain. It takes a frequency to how often the Alarm should run, which even includes enumerations of time (e.g. 1 hour/day/week).

When the Alarm runs it could poll to see what the current state is and react accordingly. For example a notification could go into the status bar and phone could make an audible noise and vibrate.The benefit of this is that the user does not have to keep the app running as the Alarm will trigger a broadcast receiver.

An example of the Alarm code: http://www.androidcompetencycenter.com/2009/02/android-basics-alarm-service/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.