IntentService连接到服务器侦听 [英] IntentService connected to Server listening

查看:179
本文介绍了IntentService连接到服务器侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I codeD的IntentService,我发送一个命令行。涉及到的活动。我希望到一个控制台程序。该活动的目的是为了做一个命令提示符,我可以从服务器发送和接收/

I coded an IntentService where I send a command line. Is related to an activity where I'm trying to program a Console. The activity's purpose is to do a 'command prompt' where I can send and receive to/from the server.

该服务的操作是:


  1. 连接到服务器

  2. 发送命令行

  3. 获取响应

  4. 提取响应用户

的主要问题是,每次发送一个命令行时间,该处有重新连接到服务器。 (因为每次我开始了整个服务时间)的如何避免这一步呢?

The main problem is that every time I send a command line, the Service has to reconnect to the server. (Cause every time I'm starting the whole Service)How can I avoid this step?

我想我每次执行其行动的时间保持的服务等待命令行发送,而不需要重新连接的。

I would like to "keep alive" the service waiting for a command line to send, without the need of reconnect every time I execute its action.

有没有办法做到这一点的动作响应?我的意思是,我启动该服务,每一个我把它的行动(的setAction),把我想要的线(putExtra)的时间。然后,我不得不再次启动该服务?

Is there any way to do it action-responsive? I mean, I start the service and every time I set its action (setAction) and put the line I want (putExtra). Then I have to start the service again?

晚安。明天我会回来的:)

Good night. Tomorrow I'll be back :)

在此先感谢!

推荐答案

由于它的一杆设计,使用 IntentService 不是IMO一个好方法。

Due to its "one shot" design, using an IntentService isn't a good approach IMO.

如果您不想每次发送命令时启动该服务,那么我建议你绑定到一个标准的服务(见<一HREF =htt​​ps://developer.android.com/guide/components/bound-services.html相对=nofollow>绑定服务)。如果您绑定到服务活动 onResume()方法和的onPause解除()你的活动将能够直接调用在<$ C方法$ C>服务。

If you don't want to start the service each time you send a command, then I'd suggest you 'bind' to a standard Service (see Bound Services). If you bind to the Service in your Activity's onResume() method and unbind in onPause() your Activity will be able to directly call methods in the Service.

您,当然,必须创建自己的工人服务来处理涉及任何工作然而,您的网络连接。如果你想如何做任何提示,看<一个href=\"https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/IntentService.java\"相对=nofollow>来源$ C ​​$ C为IntentService - 这是相当直接的。

You will, of course, have to create your own worker Thread in your Service to handle any work involving your network connection however. If you want any tips on how to do that, look at the source code for IntentService - it's fairly straight-forward.

这篇关于IntentService连接到服务器侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆