简体   繁体   English

在Android中的新线程上启动事件处理程序和类

[英]Starting event handler and class on a new thread in Android

I have two class, one is the main class and other is Bluetooth class. 我有两个班,一个是主班,另一个是蓝牙班。

Bluetooth class has methods for scanning for BLE devices, send data to the Bluetooth devices and event handler if one of the BLE devices sends data. 蓝牙类具有以下方法:扫描BLE设备,将数据发送到Bluetooth设备以及事件处理程序(如果其中一个BLE设备发送数据)。

In the main class, once I determine the number of BLE devices around me, I loop through them and try to get some information. 在主类中,一旦确定了我周围的BLE设备数量,我就会遍历它们并尝试获取一些信息。 So basically, in the loop I have to wait until I finish communication with the BLE device. 因此,基本上,在循环中,我必须等待直到完成与BLE设备的通信。 Since I'm running this on my main thread, application becomes un-responsive. 由于我是在主线程上运行此程序,因此应用程序变得无响应。

I would like to start my Bluetooth class on a different thread. 我想在另一个线程上开始我的蓝牙课程。 How will I proceed? 我将如何进行?

Will creating Bluetooth class on a Runnable method, will make my blbuetooth class's event handler to run on a new thread? 将在Runnable方法上创建Bluetooth类,是否会使我的blbuetooth类的事件处理程序在新线程上运行?

Thank you 谢谢

See the documentation for HandlerThread , Thread and Looper and ThreadPoolExecutor for options. 有关选项,请参见HandlerThreadThreadLooperThreadPoolExecutor的文档。 Creating just the Runnable will not start a new thread for you. 仅创建Runnable不会为您启动新线程。

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

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