弯针是如何知道将消息发送到处理器? [英] How the Looper knows to send the message to Handler?
问题描述
现在的问题是,在这里我告诉我的主题到使用 mHandler 的尺蠖
感谢您。 我现在用的是跌破code:
类LooperThread继承Thread {
公共处理器mHandler;
公共无效的run(){
活套prepare()。
mHandler =新的处理程序(){
公共无效的handleMessage(信息MSG){
//处理传入这里的消息
}
};
Looper.loop();
}
}
现在的问题是,在这里我告诉我的主题使用mHandler为 尺蠖?
您不必明确告诉它,因为系统(框架),它会为你。当你实例化处理程序
,它会自动获得访问您的电流发
的消息队列。引用您的评论:
系统怎么知道将消息发送给
mHandler
处理程序
?
我会下面详细了。
这是 android.os.Handler
在Android的构造函数:
mLooper = Looper.myLooper();
如果(mLooper == NULL){
抛出新的RuntimeException(
无法创建处理程序内螺纹已经不叫尺蠖prepare()。);
}
mqueue中= mLooper.mQueue;
正如你所看到的,首先它获得尺蠖
您当前的发
的。 Looper.myLooper()
的来源$ C $ c是如下:
公共静态最终活套myLooper(){
返回(活套)sThreadLocal.get();
}
据从线程本地存储获得它。后来,当你发送一个消息
本处理程序
的处理程序
实际上将自身设置为收件人的消息
:这是怎样的尺蠖
会知道,当它到达派遣消息
。具体为:
当你调用 mHandler.sendMessage()
,最终这个code运行时(许多其他的code系中):
MessageQueue队列= mqueue中;
布尔发= FALSE;
如果(排队!= NULL){
msg.target =这一点; // msg是您的Message实例
发送= queue.enqueueMessage(味精,uptimeMillis);
}
正如你所看到的,它集处理程序
实例作为消息的目标
。所以,后来,当消息
被调度,它将包含处理程序
作为其目标。这是怎样的尺蠖
就会知道哪些处理程序
它应该派遣它。具体来说,当你调用 Looper.loop()
,将出现以下情况为每个消息
实例队列
msg.target.dispatchMessage(MSG);
在在DispatchMessage()
code是如下:
公共无效在DispatchMessage(信息MSG){
如果(msg.callback!= NULL){
handleCallback(MSG);
} 其他 {
如果(mCallback!= NULL){
如果(mCallback.handleMessage(MSG)){
返回;
}
}
的handleMessage(MSG);
}
}
注意在最后一个 的handleMessage(MSG)
电话 - 这正是你的的handleMessage(MSG)
覆盖!
The question is, where I tell my Thread to use mHandler for the Looper?
Thank you. I am using the below code:
class LooperThread extends Thread {
public Handler mHandler;
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
The question is, where I tell my Thread to use mHandler for the Looper?
You don't need to tell it explicitly, because the system (framework) does it for you. When you instantiate the Handler
, it will automatically obtain access to the message queue of your current Thread
. Quoting your comment:
How the system know to send the message to the
mHandler
Handler
?
I'll detail it below.
This is the constructor of android.os.Handler
in Android:
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
As you can see, first it obtains the Looper
of your current Thread
. The source code of Looper.myLooper()
is as follows:
public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}
It obtains it from the thread local storage. Later, when you send a Message
with this Handler
, the Handler
actually sets itself as the recipient of the Message
: this is how the Looper
will know where to dispatch the Message
when it arrives. In details:
When you call mHandler.sendMessage()
, eventually this code runs (among many other code lines):
MessageQueue queue = mQueue;
boolean sent = false;
if (queue != null) {
msg.target = this; // msg is your Message instance
sent = queue.enqueueMessage(msg, uptimeMillis);
}
As you can see, it sets the Handler
instance as the target of the Message
. So, later, when the Message
is dispatched, it will contain the Handler
as its target. This is how the Looper
will know which Handler
it should dispatch it to. In details, when you call Looper.loop()
, the following happens for each of your Message
instances in the queue:
msg.target.dispatchMessage(msg);
The dispatchMessage()
code is the following:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
Notice the last handleMessage(msg)
call -- this is exactly your handleMessage(msg)
override!
这篇关于弯针是如何知道将消息发送到处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!