我有不止一个Handlers
在活动中。我在中创建所有处理程序onCreate()
的主要活动。我的理解是handleMessage()
每个处理程序的方法永远不会同时被调用,因为所有消息都放在同一个队列(Activity 线程 MessageQueue)中。因此,它们将按照放入队列的顺序执行。它们也将在主活动线程中执行。它是否正确 ?
public void onCreate() {
this.handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
//operation 1 : some operation with instanceVariable1
super.handleMessage(msg);
}
};
this.handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
//Operation 2: some operation with instanceVariable1
super.handleMessage(msg);
}
};
this.handler3 = new Handler() {
@Override
public void handleMessage(Message msg) {
//Operation 3: some operation with instanceVariable1
super.handleMessage(msg);
}
};
}
从文档中“当您创建一个新的处理程序时,它绑定到创建它的线程的线程/消息队列 - 从那时起,它将向该消息队列传递消息和可运行对象,并在它们到来时执行它们从消息队列中出来。”
所以你是对的,它们将按照你在 UI 线程上排队的顺序运行(因为你将在 onCreate 中创建它们)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)