Android 设计:如何在后台运行 3 个不同的线程来监控 3 个不同的设备

发布于 2024-11-25 07:25:42 字数 471 浏览 2 评论 0原文

我有一个应用程序,我想通过 Android 手机监控 3 个蓝牙设备的状态。为此,我需要向每个设备发送一些数据,并每 5 秒检查一次它们的响应是否正确。我希望即使应用程序不在前台也能进行此处理。我想了不同的解决方案,但我无法想出任何好的解决方案。

  • 我想到了使用IntentService。但 IntentService 使用一个线程来完成所有处理。但我想要 3 个不同的线程,因为每次检查之间的 5 秒时间很短,我无法在单个线程中的检查设备之间跳转。

  • 我想到使用 3 种不同的 IntentServices。但不确定这是否是最好的方法?

  • 我想到使用一个服务并生成 3 个线程,一个 foreach 设备。但我不确定当应用程序不在前台时这些线程是否会运行。

    运行此类应用程序的最佳设计是什么,即使该应用程序不在前台并且必须运行 3 个不同的线程,该应用程序也会在应用程序中进行后台处理。

I have an application, where I want to monitor 3 Bluetooth devices for their status from the Android Phone. For this, I need to send some data to each device and check if their response is correct every 5 seconds. And I want this processing to happen even when the application is not in Foreground. I thought of different solutions But I could not come up with any good solution.

  • I thought of using IntentService. But IntentService uses one thread for doing all the processing. But I would like 3 different threads because 5 seconds between every check is low that I cannot hop between checking devices in a single thread.

  • I thought of using 3 different IntentServices. But Not sure, if its the best way to go ?

  • I thought of using a Service and spawning of 3 threads, one foreach device. But I was not sure if these threads will run when the app is not in foreground.

    What would be the best design to run this sort of app which does background processing in a app even when the app is not in Foreground and it has to run 3 different threads.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

几度春秋 2024-12-02 07:25:43

一个服务,三个线程就是您想要的。让单个线程在多个设备上执行 I/O 看起来就像自找问题,它会创建不必要的依赖关系。无论应用程序是前台还是后台,所有线程都会执行。

Android 框架没有任何方法来阻止线程运行。线程执行由Dalvik 管理。当应用程序的主线程处于后台时,框架会停止向应用程序的主线程传递事件(BroadcastIntents 等事件除外),但它不会告诉 Dalvik 暂停该进程中的所有线程。如果这样做的话,这将违背后台处理的目的。

One Service, three Threads is what you want. Having a single thread performing I/O on multiple devices just seems like asking for problems, its creating a dependencies that are unnecessary. All Threads execute, regardless of whether the app is foreground or background.

The Android framework doesn't have any way to stop a Thread from running. Thread execution is managed by Dalvik. The framework stops delivering events to an app's main Thread when its in the background (other than events like BroadcastIntents), but it doesn't tell Dalvik to pause all threads in that process. This would kind of defeat the purpose of background processing if it did.

北笙凉宸 2024-12-02 07:25:43

您不一定需要三个线程。你肯定需要三个BluetoothSocket。您也许可以通过让一个服务从 onStartCommand() 启动一个线程并在 onDestroy() 中终止它来实现这一目标。该线程应循环并检查是否存在与每个设备的活动连接,如果没有,则应尝试重新连接。一旦建立连接,它应该根据其协议写入设备,然后从设备读取。没有什么可以阻止您在同一线程中向多个设备写入/读取。为此,您可以使用非阻塞 IO,检查每次迭代是否有字节要读取,直到找到它们,然后根据收到的内容采取操作。

就配对/绑定而言,我会在检查设备的线程之外执行此操作,因为您可能希望引导用户输入引脚/配对设备。只需让后台线程读/写三个设备中的每一个即可。您可能会发现您需要多个线程,这纯粹是一个品味问题......只需要一个线程。

You don't necessarily need three threads. You definitely need three BluetoothSocket s. You might be able to pull it off just by having one Service that starts a thread from onStartCommand() and kills it in onDestroy(). The thread should loop and check if there is an active connection to each device, and if there isn't one, it should attempt to reconnect. Once a connection is established, it should write to and then read from the device, according to its protocol. There is nothing stopping you from writing/reading to multiple devices in the same thread. You can use nonblocking IO for this, checking whether there are bytes to read on each iteration until you find them, and then taking an action based on what you receive.

As far as pairing/bonding goes, I would do that outside of the thread that checks the devices, as you might want to direct the user to enter the pin/pair the device. Just make the background thread read/write to each of the three devices. You may find that you want multiple threads, it's purely a matter of taste... Only one thread is needed.

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