Android handle-message的发送与处理案例详解
作者:灵动小溪 时间:2023-07-21 09:28:13
1、Handle,MessageQueue,Message类图
Handle: 处理消息,并提供一系列函数帮忙我们创建消息和插入消息到消息队列中
创建handle实例--PbapClientConnectionHandler
mHandlerThread = new HandlerThread("PBAP PCE handler", Process.THREAD_PRIORITY_BACKGROUND);
mHandlerThread.start();
//将这个线程设置为消息处理Looper线程
mConnectionHandler = new PbapClientConnectionHandler.Builder().setLooper(mHandlerThread.getLooper()).setContext(mService).setClientSM(PbapClientStateMachine.this).setRemoteDevice(mCurrentDevice).build();
Looper作用:Looper的prepare函数将Looper和调用prepare的线程绑定在一起,调用线程调用loop函数处理来自该消息队列的消息。
Android 系统的消息队列和消息循环都是针对具体线程的,一个线程可以存在(当然也可以不存在)一个消息队列和一个消息循环(Looper),特定线程的消息只能分发给本线程,不能进行跨线程通讯。但是创建的工作线程默认是没有消息循环和消息队列的,如果想让该线程具有消息队列和消息循环,需要在线程中首先调用Looper.prepare()来创建消息队列,然后调用Looper.loop()进入消息循环
MessageQueue:消息队列,Handle和Looper中使用的是同一个消息队列
2、发送消息
3、处理消息
looper处理消息:
loop 使消息循环起作用,取消息,处理消息
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
final Looper me = myLooper();//返回保存在调用线程TLV中的Looper对象
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;//取得Looper对象的消息队列
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block 取消息队列中的一个待处理消息
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);//调用该消息的Handle,交给它的dispatchMessage函数处理
}
}
Handle -dispatchMessage
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
//Message的callback不为空,则直接调用Message的callback来处理消息
handleCallback(msg);
} else {
if (mCallback != null) {
//Handle的全局Callback不为空
if (mCallback.handleMessage(msg)) {
return;
}
}
//调用handle子类的handleMessage来处理消息
handleMessage(msg);
}
}
Message.callback用法:将Runnable当做一个Message
Runnable线程处理使用实例
mHandler.post(new Runnable() {
@Override
public void run() {
final IBinder b = callbacks.asBinder();
});
}
来源:https://blog.csdn.net/nilingxi/article/details/100516442
标签:Android,Handle-Message
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java Servlet 实现动态验证码图片示例
2021-07-14 15:14:28
![](https://img.aspxhome.com/file/2023/8/89958_0s.png)
C#中lock用法详解
2021-06-07 23:24:05
如何将maven源改为国内阿里云镜像
2023-07-25 13:47:33
![](https://img.aspxhome.com/file/2023/9/57899_0s.png)
Java8如何使用Lambda表达式简化代码详解
2023-02-20 04:44:58
协定需要会话,但是绑定“BasicHttpBinding”不支持它或者因配置不正确而无法支持它
2023-03-17 16:44:34
一行java代码实现高斯模糊效果
2023-02-07 17:31:34
![](https://img.aspxhome.com/file/2023/5/78975_0s.jpg)
Java continue break制作简单聊天室程序
2022-12-20 01:23:01
手把手教你SpringBoot快速集成Swagger的配置过程
2023-10-30 01:16:22
![](https://img.aspxhome.com/file/2023/5/58705_0s.png)
Java对世界不同时区timezone之间时间转换的处理方法
2023-11-08 13:19:30
![](https://img.aspxhome.com/file/2023/0/72640_0s.jpg)
Spring Boot应用Docker化的步骤详解
2023-10-12 11:47:36
C#文件分割的方法
2023-09-17 22:20:56
关于springboot中对sqlSessionFactoryBean的自定义
2022-12-09 06:05:09
java网络编程基础知识介绍
2023-01-10 20:37:44
![](https://img.aspxhome.com/file/2023/7/77987_0s.jpg)
SpringBoot全局异常处理与定制404页面的方法
2021-08-07 18:54:56
![](https://img.aspxhome.com/file/2023/9/64759_0s.png)
Android开发之App widget用法实例分析
2021-09-18 16:25:54
Java 集合框架之List 的使用(附小游戏练习)
2023-11-24 10:33:40
![](https://img.aspxhome.com/file/2023/7/59947_0s.png)
c#处理3种json数据的实例
2023-06-23 19:12:47
C#实现根据银行卡卡号判断银行名
2021-08-21 07:14:00
![](https://img.aspxhome.com/file/2023/6/108706_0s.png)
详解Spring Security如何配置JSON登录
2023-02-08 17:39:07
![](https://img.aspxhome.com/file/2023/3/72353_0s.png)
C语言实现Flappy Bird小游戏
2022-03-13 13:56:24