简体   繁体   English

Android /套接字-如何从主UI线程向套接字线程发送信息?

[英]Android/Sockets - How to send information from main UI thread to socket thread?

I've been reading through forums for the past day and I can't seem to find an answer that would help solve my problem. 在过去的一天中,我一直在阅读论坛,但似乎找不到能够解决我的问题的答案。

I am trying to create an android socket/game client which: 我正在尝试创建一个android套接字/游戏客户端:

  1. Connects to the server on a new socket on a separate thread from the UI (which I want to keep open) 通过UI(位于我想保持打开状态)的单独线程上的新套接字连接到服务器
  2. Listens on the socket for messages from the server and sends it to the UI thread using a handler. 在套接字上侦听来自服务器的消息,然后使用处理程序将其发送到UI线程。
  3. Obtains information from the main UI thread (ie user inputs, gets textview, buttonclicks) and then emits the information to the server. 从主UI线程获取信息(即用户输入,获取textview,buttonclicks),然后将信息发送给服务器。
  4. Continues listening/sending for the rest of the activity. 继续收听/发送其余的活动。

I am having a lot of difficulty with implementing #3. 我在实施#3时遇到很多困难。 The socket that is initially connected to the server is in its own thread, which I want to keep open indefinitely. 最初连接到服务器的套接字在其自己的线程中,我想无限期保持打开状态。 What is the best way to get information from the main UI thread so that I can use that thread and socket to send information to the server? 从主UI线程获取信息的最佳方法是什么,以便我可以使用该线程和套接字将信息发送到服务器?

I see a lot of posts about returning information from a background thread to the UI thread but not the other way around. 我看到了很多关于将信息从后台线程返回到UI线程的文章,但并非相反。 Is there a standard way of doing this? 有这样做的标准方法吗?

Thanks! 谢谢!

I've found a solution for this where I used a BlockingQueue that is shared between the UI thread and the socket thread. 我找到了一个解决方案,其中使用了UI线程和套接字线程之间共享的BlockingQueue。 The UI thread uses queue.put() when it wants to place information that is to be emitted to the server, and the socket thread waits on queue.take() to get the information and send it to the server. UI线程在要放置要发送到服务器的信息时会使用queue.put(),并且套接字线程在queue.take()上等待获取信息并将其发送到服务器。

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

相关问题 Android将数据从主UI线程发送到另一个线程 - Android send data from main UI thread to another thread 如何使用从另一个线程收到的信息更新主UI? (虽然线程仍在运行) - How can I update the Main UI with information received from another thread? (While the thread is still running) 如何将数据从线程发送到主要活动 - how to send data from thread to main activity java android 如何将字符串从活动发送到主活动[ANDROID]中调用的线程 - How to send string from an activity to a thread called in main activity [ANDROID] Android将数据从单独的Java类中的Thread发送到UI线程 - Android send data from Thread in separate java class to UI thread Android:如何从主线程创建消息并将其传递给工作线程 - Android: How to create and pass messages to worker thread from main thread 从Android中的辅助线程调用主线程 - Call Main thread from secondary thread in Android Android服务阻塞了主要的UI线程 - Android service stopps the main UI thread Android-次要线程可在主UI线程上绘制GoogleMap - Android - Secondary Thread to draw on GoogleMap in main UI Thread 当工作线程在android中完成其工作时如何从工作线程向主线程获取通知 - How to get notification from worker thread to main thread when worker thread done with its work in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM