如何将数据从一个活动创建的后台线程发送到另一个已知处于活动状态的活动?
首先,我应该通过提供一些背景知识来澄清:我正在使用 Eclipse 开发一个绘制蓝牙数据的 Android 应用程序。它主要由蓝牙活动、蓝牙活动创建的后台线程和绘图活动组成。
我的问题的主要焦点:一旦已知绘图活动处于活动状态,我可以使用什么方法将蓝牙数据从后台线程发送到绘图活动,以便我可以绘制它?
这是我正在破解的开源代码。使用 Logcat,我知道蓝牙活动创建了一个后台线程,即使在蓝牙活动消失后也能持续生成蓝牙数据。我找到了将蓝牙数据记录到 Logcat 的后台方法。现在我想利用此方法将蓝牙数据发送到活动的绘图活动,以便我可以绘制它。
我可以告诉你什么似乎有效。感兴趣的?在绘图活动中定义静态方法plotData(BluetoothData),并从后台线程调用if。实时剪辑就好了。情节很好。但有人告诉我不必使用静态方法,因为这存在一些问题。那么我还应该使用什么?
有什么建议吗?
First, I should be clear by providing a little background: I'm using Eclipse to develop an Android application that plots Bluetooth data. It consists primarily of a Bluetooth Activity, a background thread created by the Bluetooth Activity, and a Plotting Activity.
The primary focus of my question: Once the Plotting Activity is known to be active, what approach can I use to send BluetoothData from the background thread to the Plotting Activity so I can plot it?
This is open source code that I am hacking. Using Logcat I know for a fact that the Bluetooth Activity created a background thread for continually producing BluetoothData even after the Bluetooth Activity is gone. And I have found the background method that logs the BluetoothData to Logcat. Now I want to leverage this method to send BluetoothData to the active Plotting Activity so I can plot it.
I can tell you what appears to work. Interested? Define a static method, plotData(BluetoothData) in the Plotting Activity and call if from the background thread. Clips along in real-time just fine. The plots are nice. But I've been told I shouldn't have to use static methods as though there is some problem with that. So what else should I use?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用
后台线程
。相反,您应该使用收集数据的Service
。在这种情况下,您创建的每个活动都可以绑定到该服务,并在收到新数据时收到通知。通过处理程序,服务可以轻松地将数据发送到绑定的活动。一个小问题:您能否解释一下为什么静态方法会出现问题,更准确地说:哪种问题?
You shouldn't use a
background thread
. Instead you should use aService
that collects the data. In this case every activity you create can bind to that service and be informed if new data was received. With a handler, the service can easily send the data to the activity that is binded.A little question: Can you please explain why there should be a problem with static methods and more precisely: which kind of problems?