IntentService,服务,或AsyncTask的IntentService、AsyncTask

2023-09-04 23:46:34 作者:大陆产

什么是实现这一目标的最佳途径。我有一个Android应用程序,将用我的蟒蛇服务器允许轮2手机之间的通信。大红大紫意味着它们不能互相交谈,直到轮开始,一旦他们发送一条消息不能发送另一个,直到对方回应这将开始新一轮的。

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.

我想我会用IntentService,但它似乎是错误使服务器不断地启动和停止,我不不会有担心的问题AsyncTask的或者是最好的方法来处理它。我怎么能要接收和发送信息到客户服务,服务似乎更有一种方式的东西?

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?

推荐答案

据我理解你的问题的类型是工人排队模型生产者 - 消费者模式)。 Intentservices是为了做到这一点。如果只有你需要做的多线程,你应该使用的服务。你尽可以用活动和服务使用的IBinder接口进行通信。

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的只是一个专门的线程,让您可以轻松地更新你的用户界面。但是,对于你的情况IntentService似乎是最好的选择。

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