Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntentService, Service, or AsyncTask

Tags:

android

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?

like image 947
arinte Avatar asked Jan 27 '12 17:01

arinte


People also ask

What is difference between service and AsyncTask?

service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

What is the difference between IntentService and service?

If the task doesn't require any and also not a very long task you can use service. If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread.

What is difference between service and thread?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.

Is AsyncTask deprecated?

AsyncTask is used to perform time talking operations in android, but it's marked as deprecated from android 11.


2 Answers

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.

like image 155
fedepaol Avatar answered Sep 18 '22 18:09

fedepaol


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

like image 22
SeanPONeil Avatar answered Sep 18 '22 18:09

SeanPONeil