简体   繁体   English

Android线程,服务及其之间的双向通信

[英]Android Threads, Services, and two way communication between them

I'm struggling to wrap my head around what needs to happen here. 我在努力思考这里需要发生的事情。 I'm currently working on an app that runs a service. 我目前正在运行可运行服务的应用程序。 The service when started opens a webserver that runs in a background thread. 该服务在启动时会打开在后台线程中运行的Web服务器。

At any point while this service is running the user can send commands to the device from a browser. 在运行该服务的任何时候,用户都可以从浏览器向设备发送命令。 The current sequence of events is as follows. 当前事件的顺序如下。

  1. User sends request to server 用户向服务器发送请求
  2. Server sends a message to the service via the msg handler construct, it sends data such as the url parameters 服务器通过msg处理程序构造向服务发送消息,它发送数据,例如url参数
  3. The service does what it wants with the data, and wants to send some feedback message to the user in the browser 服务根据数据执行所需的操作,并希望在浏览器中向用户发送一些反馈消息
  4. ????? ?????
  5. The server's response to the request contains a feed back message from the service. 服务器对请求的响应包含来自服务的反馈消息。

The way my functions are set up I need to pause my serve() function while waiting for a response from the service and then once the message is received resume and send an http response. 我的函数设置方式需要在等待服务响应时暂停我的serve()函数,然后在收到消息后恢复并发送http响应。

WebServer.java WebServer.java

public Response serve( String uri, String method, Properties header, Properties parms, Properties files )
{

    Bundle b = Utilities.convertToBundle(parms);
    Message msg = new Message();
    msg.setData(b);
    handler.sendMessage(msg);
    //sending a message to the handler in the service

    return new NanoHTTPD.Response();
}

CommandService.java CommandService.java

public class CommandService extends Service {

private WebServer webserver;
public Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          execute_command(msg.getData());//some type of message should be sent back after this executes

      };

Any suggestions? 有什么建议么? Is this structure the best way to go about it, or can you think of a better design that would lead to a cleaner implementation? 这种结构是解决该问题的最佳方法吗,还是您可以考虑一个更好的设计,从而实现更干净的实现?

I think the lack of answers is because you haven't been very specific in what your question is. 我认为缺少答案的原因是,您对问题的含义尚未明确。 In my experience it's easier to get answers to simple or direct questions that general architecture advice on StackOverflow. 以我的经验,在StackOverflow上获得一般体系结构建议的简单或直接问题的答案比较容易。

I'm no expert on Android but I'll give it a shot. 我不是Android方面的专家,但我会试一试。 My question is why you have a Webservice running in the background of a Service, why not just have one class, make your Service the Webservice? 我的问题是为什么您要在服务的后台运行Web服务,为什么不只有一个类使您的服务成为Web服务呢?

Regarding threading and communication and sleeping, the main thing to remember is that a webserver needs to always be available to serve new requests, whilst serving current requests. 关于线程,通信和睡眠,要记住的主要事情是,在服务当前请求的同时,Web服务器需要始终可用于服务新请求。 Other than that, it's normal that a client will wait for a thread to complete its task (ie the thread "blocks"). 除此之外,客户端通常会等待线程完成其任务(即线程“阻塞”)。 So most webservers spawn new a thread to handle each request that comes in. If you have a background thread but you block the initial thread while you wait for the background thread to complete its task, then you're no better off than just completing everything on the one thread. 因此,大多数Web服务器都会生成一个新线程来处理传入的每个请求。如果您有后台线程,但是在等待后台线程完成其任务的同时又阻塞了初始线程,那么除了完成所有任务之外,您的生活没有比这更好的了在一个线程上。 Actually, the latter would be preferable for the sake of simplicity. 实际上,为了简单起见,后者将是优选的。

If Android is actually spawning new threads for you when requests come in, then there's no need for a background thread. 如果Android确实在收到请求时为您生成了新线程,则不需要后台线程。 Just do everything synchronously on one thread and rejoice in the simplicity! 只需在一个线程上同步完成所有操作,并以简单为乐!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM