如何将数据从一个活动创建的后台线程发送到另一个已知处于活动状态的活动?

发布于 2024-11-13 06:22:24 字数 433 浏览 2 评论 0原文

首先,我应该通过提供一些背景知识来澄清:我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-11-20 06:22:24

您不应该使用后台线程。相反,您应该使用收集数据的Service。在这种情况下,您创建的每个活动都可以绑定到该服务,并在收到新数据时收到通知。通过处理程序,服务可以轻松地将数据发送到绑定的活动。

一个小问题:您能否解释一下为什么静态方法会出现问题,更准确地说:哪种问题?

You shouldn't use a background thread. Instead you should use a Service 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文