IntentService, Service or AsyncTask

What would be the best way to implement this. I have an Android app that will use my python server to provide communication between two phones in rounds. Rounds mean that they cannot talk to each other before the start of the round, and as soon as they send a message, they cannot send another until another person answers, who then starts a new round.

I thought I would use IntentService, but it seems wrong that the server is constantly starting and stopping, and I donโ€™t have to worry about problems using asynctask or is this the best way to handle this. How can I get a service that should receive and send messages to a client, it seems that services are more one-sided things?

+4
source share
4 answers

As I understand it, your problem is with the model model-model-model-model-model-producer-consumer). This requires intensive services. You should use services if and only you need multithreading. You can communicate with Activity and Service using the IBinder interface.

Asynctask are just specialized threads so you can easily update your interface. But for your case, IntentService seems to be the best option.

+2
source

Intent services are nothing more than workflows that are initiated by intent, execute their actions in a separate thread, and then close. They are designed to start and stop.

If you need to do things like http get, or in any case an interaction that does not require a server connection, use the intent services and notify your actions with broadcast events.

If your application should remain connected to the server (i.e., by permanently connecting to tcp), then what should I go for is to have a service (and not intentional) that performs network interaction using an asynthetic or more classical stream, hosted by the service. You can then activate the action using the bindToService () service.

I would recommend not using asynctasks inside the action. You risk losing the serverโ€™s response in case of changes in the horizontal / vertical view, as indicated in its answer1.

+17
source

I highly recommend the IntentService / Broadcast Receiver route. Avoiding unpleasant AsyncTask configuration problems will make your life ten times easier.

+5
source

I would use Alarm, which is scheduled through AlarmManager , since then it can be set to check the start of the round / queue. It has the advantages of a service, but not the horrors of battery drain. The frequency of how often the Alarm should be triggered is required, which also includes time transfers (for example, 1 hour / day / week).

When Alarm is triggered, it can interrogate to find out what the current state is and react accordingly. For example, a notification may enter the status bar, and the phone may make audible noise and vibration. The advantage of this is that the user does not need to support the application because the alarm triggers the broadcast receiver.

Example alarm code: http://www.androidcompetencycenter.com/2009/02/android-basics-alarm-service/

0
source

Source: https://habr.com/ru/post/1393406/


All Articles