Handler handler = new Handler(Looper.myLooper()){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case WHAT_BT_CONNECT_OK: broadcastEvent(OnEventListener.CONNECT_SUCCESS); break; case WHAR_BT_DISCONNECT_OK: broadcastEvent(OnEventListener.DISCONNECT_SUCCESS); break; case WHAT_BT_ACCEPT_OK: broadcastEvent(OnEventListener.ACCEPT_SUCCESS); break; case WHAT_BT_CONNECT_NG: broadcastEvent(OnEventListener.CONNECT_FAILED); break; case WHAT_BT_RECV_DATA: broadcastReceivedData((String)msg.obj); break; case WHAT_BT_RECV_INTERUPT: broadcastEvent(OnEventListener.BREAK_OFF); break; } } };
时间: 2024-02-10 14:19:44 浏览: 40
这段代码定义了一个Handler对象,用于处理消息。在这个Handler中,通过重写handleMessage方法,通过switch语句处理不同的消息,根据消息的what属性值,执行不同的操作,其中包括广播不同的事件,比如连接成功、断开成功、接收成功等,以及接收到的数据。这个Handler可以在Android中用于处理各种异步操作的结果返回。
相关问题
private static final int WHAT_BT_ACCEPT_OK = 3; Handler handler = new Handler(Looper.myLooper()){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case WHAT_BT_ACCEPT_OK: BluetoothSocket socket = (BluetoothSocket) msg.obj; if(onAcceptListener != null) onAcceptListener.onAccepted(socket); break; } } };
这段代码定义了一个 `Handler` 对象 `handler`,用于处理蓝牙通信过程中的消息。在构造函数中,通过 `Looper.myLooper()` 获取当前线程的消息队列,并将其作为参数传入,以创建一个与当前线程关联的 `Handler` 对象。在 `handleMessage()` 方法中,根据传入的消息类型 `msg.what` 进行不同的处理。如果消息类型是 `WHAT_BT_ACCEPT_OK`,则将消息中的 `obj` 强制转换为 `BluetoothSocket` 对象,并调用 `onAcceptListener` 的 `onAccepted()` 方法,将该 `BluetoothSocket` 对象作为参数传递给它。其中,`onAcceptListener` 是一个自定义的接口,用于处理蓝牙通信的连接请求。
private Handler uiHandler = new Handler() { @Override public void handleMessage(Message msg) { //定义一个弹出层dialog AlertDialog alert = new AlertDialog.Builder(mContext).create();
这段代码是一个Android程序中的Handler类的实现,用于在UI线程中处理来自其他线程的消息。在这个例子中,当Handler接收到一条消息时,它会创建一个AlertDialog弹出框,并将其显示在应用程序的上下文中。这个弹出框可以用来显示一些消息或请求用户做出某些操作。具体来说,AlertDialog.Builder类是用来构建AlertDialog对象的,它需要传递一个Context对象作为参数。在这个例子中,mContext是一个成员变量,它是在Handler的构造函数中初始化的,表示这个Handler的上下文对象。由于这个Handler是在UI线程中创建的,所以它的上下文对象也是UI线程的上下文对象,因此可以在其中创建UI元素,如AlertDialog。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)